February 28, 2021
Hot Topics:

Introduction to the Java Robot Class in Java

  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Discussion and Sample Code

I will begin with a discussion of the program named Robot04, followed by a discussion of the program named Robot05.

Description of the program named Robot04

The Robot class makes it possible for your Java program to temporarily take control of the mouse and keyboard input functionality at the operating-system level.

(In fact, if you accidentally allow your program to take control of the mouse and the keyboard in an infinite loop, the program can be very difficult to terminate.)

This program illustrates the low-level nature of an object of the Robot class.  In this program, a Robot object is designed to click the Close button on a non-Java program (such as Internet Explorer) causing that program to terminate.

Should work in Windows 1024 by 768 mode

This program should run successfully without modification if you are running Windows with a screen resolution of 1024 by 768 pixels.  If you are running under a different operating system, or a different screen resolution, you may need to modify the coordinate values in the program to cause this program to behave as described.  (A comment is provided in the code to show you where to make the modification.)

Open Internet Explorer in full-screen mode

To see the intended behavior of this program, start Internet Explorer, (or some other program with a Close button in its upper right corner) and maximize it to full-screen mode.  This should cause the Close button with the X for that program to appear in the upper right-hand corner of the screen.

This program will click the Close button

Then run this program at the command prompt.  There is no GUI for this program.  The program simply starts running, performs the programmed action, and terminates.

If all goes well, this program will use an object of the Robot class to position the mouse pointer over the Close button belonging to the other program.  Then this program will use the Robot object to click that button.  The result of clicking the button will be the same as if a human user had clicked the Close button on that program.

(Frequently, the result of clicking the Close button on a program's main GUI is to terminate the program.  That is the case with Internet Explorer.  However, other programs, such as Windows Notepad, may bring up a dialog asking the user to confirm something such as saving a file.  This program is not designed to deal with such a dialog.)

If things don't work as described ...

If you don't get this result, try adjusting the coordinate values in the program to make certain that the mouse pointer is automatically positioned on the Close button before the mouse click is executed.

(Delays are inserted in the behavior of this program so that you can view the process in slow motion.)

Requires version 1.3 or later

The Robot class was first released in SDK Version 1.3, so you will need to be running Version 1.3 or later.  This program was tested on my machine using SDK 1.4.1 under WinXP

The main method for the program named Robot04

I will discuss this program in fragments.  A complete listing of the program is shown in Listing 10 near the end of the lesson.

The program begins in Listing 1 where the main method starts by instantiating a new object of the Robot class.

public class Robot04{
  public static void main(String[] args)
                             throws AWTException{
    Robot robot = new Robot();

Listing 1

Mouse and keyboard input under program control

As mentioned earlier, an object of the Robot class makes it possible for your program to temporarily gain control of the mouse and keyboard.

(The Robot class also provides some other interesting features, such as the ability to create an image containing pixels read from the screen, which are not discussed in this lesson.  See Sun's documentation for a description of those features.)

Several instance methods are available

The Robot class provides several instance methods, (including the following), by which your program can produce mouse and keyboard input, just as though that input were being provided by a human user.

  • mouseMove - Moves the mouse pointer to a set of specified absolute screen coordinates given in pixels.
  • mousePress - Presses one of the buttons on the mouse.
  • mouseRelease - Releases one of the buttons on the mouse.
  • keyPress - Presses a specified key on the keyboard.
  • keyRelease - Releases specified key on the keyboard.

Move the mouse pointer

The code in Listing 2 causes the mouse pointer to move to a location 1005 pixels to the right and 10 pixels down from the upper left-hand corner of the screen.

(If you watch carefully while the program is running, you will see the mouse pointer move to this location.

If your program attempts to move the mouse pointer to a location specified by negative coordinates, it will move to the 0,0 location in the upper-left corner of the screen.  If your program moves the mouse pointer off the screen with positive coordinates, the pointer will simply disappear.)


Listing 2

Should point to the Close button of Internet Explorer

For a screen resolution of 1024 by 768 with a Windows look and feel, the coordinates 1005, 10 should cause the mouse pointer to point to the Close button on a full-screen version of Internet Explorer.  If this is not the case on your system, you may need to modify these coordinate values to cause the mouse to point to the Close button.

Insert a delay

The code in Listing 3 inserts a delay in the process by causing the thread to sleep for two seconds (2000 milliseconds).  This should make it possible for you to view the pointer in its new location before anything else happens.


Listing 3

What about InterruptedExceptions?

Sometimes when you put a thread to sleep, you may want to be able to catch InterruptedExceptions that may occur on that thread.  If you have that need, you should use the sleep method of the Thread class instead of the delay method of the Robot class.

Press the left mouse button and delay again

The code in Listing 4 invokes the mousePress method on the Robot object to execute a press of the left mouse button at the current position of the mouse pointer.


Listing 4

The mousePress method accepts an incoming int parameter that specifies which mouse button is pressed.  Suitable values for specifying the left, center, and right mouse buttons are found as constants in the InputEvent class as shown below:

  • InputEvent.BUTTON1_MASK
  • InputEvent.BUTTON2_MASK
  • InputEvent.BUTTON3_MASK

(Note that the Robot class was updated in SDK V1.4 to support a mouse wheel.  According to Sun, the mouseWheel method of the Robot class "Rotates the scroll wheel on wheel-equipped mice."  That capability is not illustrated in this lesson.)

Does anything interesting happen?

Whether or not something interesting happens as a result of invoking the mousePress method in Listing 4 depends on what the mouse pointer is pointing to when the method is invoked.  The same thing will happen that would happen if a human user were to press the left mouse button when the mouse pointer is pointing to the same location on the screen.

If the pointer happens to be pointing at a sensitive location on the screen, something interesting will happen.  If the mouse pointer happens to be pointing to a non-sensitive location on the screen, the mouse press will simply be ignored and nothing interesting will happen.

(As an interesting exercise, try modifying this program to invoke a right-button click (BUTTON3_MASK) on a blank area of a Windows screen.  This should cause a typical Windows popup menu to appear.)

Another delay

The code in Listing 4 includes another two-second delay.  If the mouse pointer is pointing to the Close button when the mousePress method is invoked, you should be able to see the Close button in its depressed state for two seconds. 

Does not terminate Internet Explorer

Simply pressing the left mouse button on the Close button on Internet Explorer won't terminate the program.  A mouse press followed by a mouse release is required to terminate the program.

Release the left mouse button

The code in Listing 5 invokes the mouseRelease method causing the program to behave as if a human user has released the left mouse button (after first pressing that button).

  }//end main

}//end class Robot04

Listing 5

Assuming that the mouse pointer is pointing to the Close button on Internet Explorer, the code in Listing 5 should cause the Close button to pop back out and should cause the Internet Explorer program to terminate.  (However, it will probably happen so fast that you probably won't see the button pop back out.)

The code in Listing 5 also ends the main method, ends the class definition, and thus ends the program.

Recap for the program named Robot04

The Java program named Robot04 causes the mouse to move to a location that is believed to be the location of the Close button for a running instance of Internet Explorer.

(In the next lesson, I will show you how to determine the location of components in a Java GUI with more certainty.)

Having moved the mouse pointer to that location, Robot04 clicks the left mouse button in an attempt to cause the running instance of Internet Explorer to terminate.  If the location of the mouse pointer was correct, Internet Explorer will terminate.

Page 2 of 4

This article was originally published on May 27, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date