How to create automating GUI programs with java.awt.Robot
Imagine that you have a Java GUI program written using the AWT and
Swing libraries, and you'd like to automate the program. For example,
you'd like to automatically supply GUI input events from the keyboard
and mouse to the program. In this way, the program could operate without
user intervention. This type of automation might be useful for testing
purposes, or to produce a self-running demo program.
java.awt.Robot is a new class in JDK 1.3, designed to handle this
type of automation. It's a way to automatically feed input events
to a program. The events are generated in the native input queue of
the platform, as if they had actually been generated by the user.
For example, using java.awt.Robot, you can generate an event that
is equivalent to a user moving the mouse to particular (X,Y) coordinates
on the screen.
Here's an example of how you can use this class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RobotDemo {
public static void main(String args[]) throws AWTException {
// set up frames and panels
JFrame frame = new JFrame("RobotDemo");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 1));
// set up fields, labels, and buttons
final JTextField field = new JTextField(10);
final JLabel lab = new JLabel();
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "Length: " +
field.getText().length();
lab.setText(s);
}
});
JButton button = new JButton("Exit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// add components to panel and display
panel.add(field);
panel.add(lab);
panel.add(button);
frame.getContentPane().add(panel);
frame.setSize(200, 150);
frame.setLocation(200, 200);
frame.setVisible(true);
// create a robot to feed in GUI events
Robot rob = new Robot();
// enter some keystrokes
int keyinput[] = {
KeyEvent.VK_T,
KeyEvent.VK_E,
KeyEvent.VK_S,
KeyEvent.VK_T,
KeyEvent.VK_I,
KeyEvent.VK_N,
KeyEvent.VK_G
};
rob.delay(1000);
rob.keyPress(KeyEvent.VK_SHIFT);
field.requestFocus();
for (int i = 0; i < keyinput.length; i++) {
rob.keyPress(keyinput[i]);
rob.delay(1000);
}
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_ENTER);
// move cursor to Exit button
Point p = button.getLocationOnScreen();
rob.mouseMove(p.x + 5, p.y + 5);
rob.delay(2000);
// press and release left mouse button
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.delay(2000);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
The demo sets up a panel containing an input field, a label, an
Out&In button and an Exit button. This part of the demo is typical
of many Swing applications. Then the demo creates a Robot object,
and feeds into it a series of keystrokes. These keystrokes mimic
keys typed by a user, that is, the keys T, E, S, T, I, N, and G.
There is a delay of 300 milliseconds between keystrokes; this helps
present the animation more clearly. The shift key is held down throughout,
so that the letters are entered as capitals. At the end of the text
input, Enter is specified. This causes the length of the input to
be echoed in the label field. Then the mouse cursor is moved to
the Out&In button and left mouse button is pressed and released(clicked).
The mouse cursor is moved out the frame and back, finally it is
moved to the Exit button and clicked the button. This terminates
the program.
Notice that virtual keycodes are used to enter keystrokes; keycodes
are not the same as Java characters. KeyEvent.VK_A corresponds to
pressing the unshifted 'A' key on a keyboard. If you specify 'A'
or 'a' instead of KeyEvent.VK_A, you get unexpected results.
Also note that the documentation for Robot says that some platforms
require special privileges to access low-level input control. One
specific case is X Windows, which requires the XTEST 2.2 standard
extension.
|