Introduction to the Java Robot Class in Java
Java Programming Notes # 1472
- Discussion and Sample Code
- Run the Program
- What's Next?
- Complete Program Listings
Programming in Java doesn't have to be dull and boring. In fact, it's possible to have a lot of fun while programming in Java. This is the first lesson in a miniseries that show you how to use the Robot class to write programs that are both fun and useful.
New features in SDK Version 1.3
One of the new features that was released in SDK Version 1.3 was the Robot class. According to Sun,
"This class 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."
Different from the system event queue
I have published several tutorial lessons that show you how to create events and post them to the system event queue. (For example, see lesson 104 at http://www.dickbaldwin.com/tocmed.htm.) However, it is important to note that using a Robot object to fire events is significantly different from posting events to the system event queue.
Here is how Sun compares the Robot class and the system event queue.
"Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events."
Potential problem areas
Basically, a Robot object makes it possible for your program to temporarily take over control of the mouse and the keyboard. However, this may not be possible on all operating systems. For example, Sun has this to say.
"Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects."
Sun goes on to provide some examples of cases where an AWTException might be thrown.
A word of caution
A runaway Java Robot object has the ability to wrest control away from the human user, so you need to be a little careful. For example, if you allow your Java Robot program to go into an infinite loop, making mouse moves, clicking the mouse, and entering keystrokes, you may find that the only practical way to regain control of your computer is to either turn off the power or press the reset button to force your computer to restart.
Three lessons in the miniseries
According to my current planning, this miniseries on the Robot class will consist of three lessons. This first lesson will demonstrate the low-level nature of the behavior of an object of the Robot class. This will be accomplished by showing you how to create a Java robot that can manipulate other non-Java programs, such as Windows Notepad and Internet Explorer.
The second lesson in the 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.
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.
I will discuss two sample programs named Robot04 and Robot05 in this lesson. The sample programs will illustrate:
- 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.