The AWT Robot!
This topic may not be of immediate use to everyone, but
sometimes an API is just interesting enough that it deserves mentioning.
In Java 1.3, a class with the intriguing name java.awt.Robot was added.
The AWT robot provides an API for generating input events such as
keystrokes and mouse gestures programmatically. It could be used to build
automated GUI testing tools and the like. The following example uses the
Robot class to move the
mouse to the upper-left area of the screen and perform a series of events
corresponding to a double-click. On most Windows systems, this opens up
the My Computer folder that lives in that region of
the screen.
publicclassRobotExample{publicstaticvoidmain(String[]args)throwsException{Robotr=newRobot();r.mouseMove(35,35);r.mousePress(InputEvent.BUTTON1_MASK);r.mouseRelease(InputEvent.BUTTON1_MASK);Thread.sleep(50);r.mousePress(InputEvent.BUTTON1_MASK);r.mouseRelease(InputEvent.BUTTON1_MASK);}}
In addition to its magic fingers, the AWT robot also has eyes! You
can use the Robot class to capture an
image of the screen or a rectangular portion of it by using the createScreenCapture()
method. (Note that you can get the exact dimensions of the screen from the
AWT’s getScreenSize() method.)
Java 5.0 added a correspondingly useful API, java.awt.MouseInfo, which allows the gathering of mouse movement information from anywhere on the screen (not restricted to the area within the Java application’s windows). The combination ...