The Robot class in the Java AWT package 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. In simple terms, the class provides control over the mouse and keyboard devices.
Here is an example of how the Robot class takes control of the keyboard and types out into a blank Notepad document. Notepad is called using Process and Runtime, as discussed in this article.
The code opens a blank Notepad file and types "hello from geeksforgeeks" onto it with a delay of 500 ms before typing out each character.
Methods of the Robot class
|BufferedImage||createScreenCapture(Rectangle screenRect)||Creates an image containing pixels read from the screen.|
|void||delay(int ms)||Sleeps for the specified time.|
|int||getAutoDelay()||Returns the number of milliseconds this Robot sleeps after generating an event.|
|Color||getPixelColor(int x, int y)||Returns the color of a pixel at the given screen coordinates.|
|boolean||isAutoWaitForIdle()||Returns whether this Robot automatically invokes waitForIdle after generating an event.|
|void||keyPress(int keycode)||Presses a given key.|
|void||keyRelease(int keycode)||Releases a given key.|
|void||mouseMove(int x, int y)||Moves mouse pointer to given screen coordinates.|
|void||mousePress(int buttons)||Presses one or more mouse buttons.|
|void||mouseRelease(int buttons)||Releases one or more mouse buttons.|
|void||mouseWheel(int wheelAmt)||Rotates the scroll wheel on wheel-equipped mice.|
|void||setAutoDelay(int ms)||Sets the number of milliseconds this Robot sleeps after generating an event.|
|void||setAutoWaitForIdle(boolean isOn)||Sets whether this Robot automatically invokes waitForIdle after generating an event.|
|void||waitForIdle()||Waits until all events currently on the event queue have been processed.|
This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java Robot Class | Get the pixel Color of a given point
- Automate mouse events using Java Robot Class
- JSwing | Create a Magnifying tool using Java Robot
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 1