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.

 public class RobotExample
 {
 public static void main( String [] args ) throws Exception
 {
 Robot r = new Robot( );
 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, 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 app's windows). The combination of Robot and MouseInfo should make it easier to record and play back events occurring anywhere on the screen from within Java.

Java ScreenShot
Comments