January 25, 2021
Hot Topics:

Introduction to the Java Robot Class in Java

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

Run the Program

I encourage you to copy the code from Listings 10 and 11 into your text editor, compile it, and execute it.  Experiment with it, making changes, and observing the results of your changes.

Remember, however, that you must be running Java V1.3 or later to compile and execute these programs, because they make use of Java features that were first released in V1.3.  (In case you decide to make use of the mouseWheel method of he Robot class, you must be running SDK V1.4 or later.)


In this lesson, I have taught you:

  • How to start a non-Java program running in a separate process.
  • How to programmatically invoke mouse clicks on non-Java programs with the result being the same as that, which would be experienced if a human user were to produce those same mouse clicks.
  • How to programmatically enter text into a non-Java program that is running and ready to accept keyboard input.

What's Next?

The next lesson in this miniseries will show you how to use a robot to perform automatic testing on a Java GUI.

The third lesson will show you how to write an animated robot program to provide a visual demonstration of the use of a Java GUI.

Complete Program Listings

Complete listings of the programs discussed in this lesson are shown in Listings 10 and 11 below.
/*File Robot04.java
Copyright 2003 R.G.Baldwin

This program illustrates the low-level nature of
the behavior of an object of the Robot class by
programming a Robot object to click the Close
button of a non-Java program (such as Internet
Explorer) and causing that program to terminate.

This program should work without modification if
you are running WinXP with 1024 by 768 screen
resolution.  If you are running under a different
OS, or a different screen resolution, you may
need to modify the coordinate values in the

To see the desired behavior of this program,
start Internet Explorer, (or some other program)
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.

Then run this program.  If all goes well, this
program will use an object of the Robot class
to position the mouse pointer over the Close
button of the other program and then click that
button.  The result will be the standard result
of clicking the Close button on that program.
Frequently, the standard result of clicking the
Close button is to terminate the program.

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 so that
you can view the process in slow motion.

Tested using SDK 1.4.1 under WinXP

import java.awt.*;
import java.awt.event.*;

public class Robot04{
  public static void main(String[] args)
                             throws AWTException{
    Robot robot = new Robot();
    //You may need to modify these coordinates if
    // they fail to place the mouse pointer on
    // the Close button of the non-Java program
    // that you are trying to terminate.

    //Press and then release the left mouse
    // button.  The delays are provided so that
    // you can view the action.  Watch the upper-
    // right corner of the screen when you run
    // this program.
  }//end main

}//end class Robot04

Listing 10


/*File Robot05.java
Copyright 2003 R.G.Baldwin

Note:  As written, this program will only run
correctly under the Windows operating system,
because it makes use of the Windows program named
Notepad.  However, it could easily be modified to
use a similar program under a different operating

This program illustrates the low-level nature of
the behavior of an object of the Robot class by:

1. Starting the Windows Notepad editor program
in a separate process.
2. Programming a Robot object to enter text into
the Notepad document.

To see the desired behavior of this program,
simply start the program running under the Windws
operating system.

When the program starts running, it causes the
Windows Notepad program to start running with
a new empty document.

Then this program uses a Robot object to enter
the word Hello into the Notepad document, and

At this point, the Notepad program is still
running, and can be saved to a file, or
terminated by the user.

Tested using SDK 1.4.1 under WinXP

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class Robot05{
  //Create an array of keycode data
  int keyInput[] = {
  };//end keyInput array

  public static void main(String[] args)
                 throws AWTException,IOException{

    //Start the Windows Notepad program running
    // in a separate process.  It should become
    // the active window, capable of accepting
    // input from the keyboard.

    //Get a Robot object that will be used to
    // enter characters into the Notepad document
    Robot robot = new Robot();

    //Enter the keycodes contained in the
    // keyInput array into the Notepad document.
    // Make the first character upper case and
    // the remaining characters lower case.
    for (int cnt2 = 0;
                 cnt2 < keyInput.length; cnt2++){
      if(cnt2 > 0){
      }//end if
      //Insert a one-half second delay between
      // characters.
    }//end for loop
}//end class Robot05

Listing 11

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.


Copyright 2003, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.


Page 4 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