Automating Programs with Robots
From time to time, software developers need to automate software execution (as in self-running demos) and testing. In each case, a robot (an autonomous program) programmatically delivers events describing keystrokes, mouse clicks, and mouse movements to the program being controlled.
Version 1.3 of the Java 2 Platform Standard Edition introduces a class called Robot. The methods in this class programmatically deliver keystroke and mouse events to the native windowing system's event queue.
To use Robot, you must first create an object. For example, the following code fragment creates a Robot object.
Robot r = new Robot ();
After you have a Robot object, you can issue method calls. For example, you can send keystroke events by calling Robot's keyPress and keyRelease methods. Furthermore, you can send mouse events by calling the mouseMove, mousePress, and mouseRelease methods. Additional methods range from retrieving a pixel's color to waiting between events. (For more information, check out the SDK documentation.)
To demonstrate robots, Listing 1 presents source code for a RobotDemo application.
Listing 1 The RobotDemo application source code
// RobotDemo.java import java.awt.*; import java.awt.event.*; class RobotDemo extends Frame implements ActionListener { RobotDemo (String title) { super (title); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } }); Panel p = new Panel (); Button b = new Button ("Press Me"); b.addActionListener (this); p.add (b); add (p); setSize (175, 100); setVisible (true); } public void actionPerformed (ActionEvent e) { try { Runtime.getRuntime ().exec ("notepad.exe"); } catch (java.io.IOException e2) { System.out.println (e2);} try { Thread.sleep (1000); } catch (InterruptedException e2) {} try { Robot r = new Robot (); int [] keys = { KeyEvent.VK_T, KeyEvent.VK_E, KeyEvent.VK_X, KeyEvent.VK_T, KeyEvent.VK_ENTER }; for (int i = 0; i < keys.length; i++) { r.keyPress (keys [i]); r.keyRelease (keys [i]); } Toolkit tk = Toolkit.getDefaultToolkit (); Dimension dim = tk.getScreenSize (); r.mouseMove (dim.width / 2, dim.height / 2); } catch (AWTException e2) {} } public static void main (String [] args) { new RobotDemo ("Robot Demo"); } }
RobotDemo's main thread creates a GUI with a single button. After this button is pressed, RobotDemo's event-dispatching thread attempts to launch the Notepad program on a Windows platform. (If you want to play with RobotDemo on some other platform, replace notepad.exe with the platform-specific name of a more appropriate program.) After the program is started, RobotDemo's event-dispatching thread sleeps for one second.
Note: The reason for sleeping is to give the program a chance to activate. For slower computers, you might need to increase the sleep interval.
After the event-dispatching thread wakes up, it issues keystrokes that spell out the word text. A carriage-return keystroke is then issued. At this point, the event-dispatching thread obtains the screen size and moves the mouse pointer to its center.
Note: Some platforms require special privileges before a Robot object can be created. If these privileges are not present, Robot's constructors are designed to throw an AWTException object.