Skip to content
Related Articles

Related Articles

Robot Class in Java AWT
  • Difficulty Level : Easy
  • Last Updated : 29 May, 2017

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 program to demonstrate working of Robot
// class. This program is for Windoes. 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 mili 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.

Methods of the Robot class



Return TypeMethodDescription
BufferedImagecreateScreenCapture(Rectangle screenRect)Creates an image containing pixels read from the screen.
voiddelay(int ms)Sleeps for the specified time.
intgetAutoDelay()Returns the number of milliseconds this Robot sleeps after generating an event.
ColorgetPixelColor(int x, int y)Returns the color of a pixel at the given screen coordinates.
booleanisAutoWaitForIdle()Returns whether this Robot automatically invokes waitForIdle after generating an event.
voidkeyPress(int keycode)Presses a given key.
voidkeyRelease(int keycode)Releases a given key.
voidmouseMove(int x, int y)Moves mouse pointer to given screen coordinates.
voidmousePress(int buttons)Presses one or more mouse buttons.
voidmouseRelease(int buttons)Releases one or more mouse buttons.
voidmouseWheel(int wheelAmt)Rotates the scroll wheel on wheel-equipped mice.
voidsetAutoDelay(int ms)Sets the number of milliseconds this Robot sleeps after generating an event.
voidsetAutoWaitForIdle(boolean isOn)Sets whether this Robot automatically invokes waitForIdle after generating an event.
voidwaitForIdle()Waits until all events currently on the event queue have been processed.

References:

  • https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html

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 contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :