Robot Class in Java with Example by Pratik Butani

Java Robot Class Programming - Pratik Butani

Robot Class in Java

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.
More about Robot class Click Here:
 

Example of Robot Class:

 
I have created simple example for Robot class using KeyBoard Events:
 
Just Follow this Step:
  • Copy/Paste whole code in Notepad or any Editor.
  • Save with namedJavaRobotExample.java
  • Run it
  • You may ask for Editor name to run in your computer for e.g. notepad, wordpad, winword etc. whatever you can write in Run window.
  • press Enter, now don’t touch keyboard or mouse and just wait for while Running… Typing… Saving…
import java.awt.AWTException;
import java.awt.Robot;
//import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;


public class JavaRobotExample {
    Robot robot = new Robot();
    Scanner scanner;


    public static void main(String[] args) throws AWTException {
        new JavaRobotExample();
    }

    public JavaRobotExample() throws AWTException {
        scanner = new Scanner(System.in);
        String get;

        System.out.print("nEnter Program Name to Run In Your Computer... :- ");
        get = scanner.next();

        robot.setAutoDelay(50);
        robot.setAutoWaitForIdle(true);

        robot.delay(100);
        robot.keyPress(KeyEvent.VK_WINDOWS);
        robot.delay(500);
        type("R");
        robot.delay(500);
        robot.keyRelease(KeyEvent.VK_WINDOWS);

        robot.delay(500);
        type(get);
        robot.delay(1000);
        type(KeyEvent.VK_ENTER);

        robot.delay(5000);

        type("Hello to everyone...!!");

        type(KeyEvent.VK_ENTER);
        robot.delay(50);
        type(KeyEvent.VK_ENTER);

        robot.delay(100);
        type("This is a Robot Class Presentation in JAVA...!!!");
        type(KeyEvent.VK_ENTER);
        type(KeyEvent.VK_ENTER);

        robot.delay(100);
        type("Created By - Pratik Butani- Flutter Developer");
        type(KeyEvent.VK_ENTER);
        type(KeyEvent.VK_ENTER);
        robot.delay(100);

        type("- 7Span - Ahmedabad");
        type(KeyEvent.VK_ENTER);
        type(KeyEvent.VK_ENTER);

        robot.delay(100);
        type("Contact No.- 88 66 22 45 46");
        type(KeyEvent.VK_ENTER);
        type(KeyEvent.VK_ENTER);
        type("Thank You..!!!");

        robot.delay(500);
        robot.keyPress(KeyEvent.VK_CONTROL);

        type("s");
        robot.keyRelease(KeyEvent.VK_CONTROL);

        robot.delay(1000);
        type("pratik");
        robot.delay(500);
        type(KeyEvent.VK_ENTER);

        robot.delay(1000);
        type("y");
        robot.delay(1000);

        robot.delay(1000);
        type(KeyEvent.VK_ALT);
        robot.delay(1000);
        type("f");
        robot.delay(1000);
        type("x");

        type("n");

        System.exit(0);
    }

    private void type(int i) {
        robot.delay(50);
        robot.keyPress(i);
        robot.keyRelease(i);
    }

    private void type(String s) {
        byte[] bytes = s.getBytes();
        for (byte b : bytes) {
            int code = b;
            // keycode only handles [A-Z] (which is ASCII decimal [65-90])
            if (code > 96 && code < 123) code = code - 32;
            robot.delay(40);
            robot.keyPress(code);
            robot.keyRelease(code);
        }
    }
}

Have fun <3

Thank you 🙂

Checkout my trending post : http://www.pratikbutani.com/2017/04/android-studio-productivity-top-20-shortcuts-for-android-developers/

Page Views (110)

1217 Total Views 1 Views Today
Share Love & Support:

About Pratik Butani

Pratik Butani is Enthusiastic Android Application Developer, Speaker at Google Developer Group – Rajkot, All time Learner of new things, Googler, Eager to Help IT Peoples. He also like to TEACH Android to New Learner and Share his knowledge to them. He is currently working as a Senior Android Developer at Soham ERP Solutions Pvt. Ltd. – Ahmedabad. He is working with Android Development since 2013 and now Flutter is on going.

Comment Your Suggestion or Appreciation:

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *