Robot Class in Java AWT - GeeksforGeeks

12 May, 2022
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.
Java
// Java program to demonstrate working of Robot// class. This program is for Windows. It opens// notepad and types a message.import java.awt.AWTException;import java.awt.Robot;import java.awt.event.KeyEvent;import java.io.*; public class robo{    public static void main(String[] args) throws IOException,                           AWTException, InterruptedException    {        String command = "notepad.exe";        Runtime run = Runtime.getRuntime();        run.exec(command);        try {            Thread.sleep(2000);        }        catch (InterruptedException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }         // Create an instance of Robot class        Robot robot = new Robot();         // Press keys using robot. A gap of        // of 500 milli seconds is added after        // every key press        robot.keyPress(KeyEvent.VK_H);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_E);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_L);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_L);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_O);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_SPACE);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_F);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_R);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_O);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_M);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_SPACE);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_G);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_E);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_E);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_K);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_S);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_F);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_O);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_R);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_G);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_E);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_E);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_K);        Thread.sleep(500);        robot.keyPress(KeyEvent.VK_S);    }}
Output: 
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.
 
You might have already found out that the code has become really lengthy for writing a small string. In that case we can store the message in a string and iterate it and display. To press the capital letters we have to press down the shift key, press the key and then release the shift key. The same can be done for symbols too.
Java
import java.awt.*;import java.awt.event.KeyEvent; class Robo {    // Our custom sleep method    public static void sleep(long ms) {        try {Thread.sleep(ms);} catch (Exception ignored) {}    }     public static void main(String[] args) throws Exception {        // Open Notepad        Runtime.getRuntime().exec("notepad.exe");        // Wait for 2 seconds        sleep(2000);        // Create instance of Robot class        Robot robot = new Robot();        // The String to type        String str = "Hello from GeeksforGeeks";        // Press keys using robot        // A gap of 200ms is added between each key press        for (int i = 0; i < str.length(); i++) {            // Check if the current character is a capital letter            if (Character.isUpperCase(str.charAt(i))) {                // Press shift key                robot.keyPress(KeyEvent.VK_SHIFT);                // Press the current character                robot.keyPress(Character.toUpperCase(str.charAt(i)));                // Release shift key                robot.keyRelease(KeyEvent.VK_SHIFT);            }            // else display the character as it is            else robot.keyPress(Character.toUpperCase(str.charAt(i)));            // wait for 200ms            sleep(200);        }    }}
This opens the Notepad and types the same thing with a 200ms gap between characters. This approach is better when working with larger items.
This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
simranarora5sos
ruthvikas2431
Java-Library
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Multithreading in Java
Initializing a List in Java