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.
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.
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. For example, X-Window systems
will throw the exception if the XTEST 2.2 standard extension is not supported
(or not enabled) by the X server.
Applications that use Robot for purposes other than self-testing should
handle these error conditions gracefully.
| Method from java.awt.Robot Detail: |
public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
checkScreenCaptureAllowed();
// according to the spec, screenRect is relative to robot's GD
Rectangle translatedRect = new Rectangle(screenRect);
translatedRect.translate(gdLoc.x, gdLoc.y);
checkValidRect(translatedRect);
BufferedImage image;
DataBufferInt buffer;
WritableRaster raster;
if (screenCapCM == null) {
/*
* Fix for 4285201
* Create a DirectColorModel equivalent to the default RGB ColorModel,
* except with no Alpha component.
*/
screenCapCM = new DirectColorModel(24,
/* red mask */ 0x00FF0000,
/* green mask */ 0x0000FF00,
/* blue mask */ 0x000000FF);
}
int pixels[];
int[] bandmasks = new int[3];
pixels = peer.getRGBPixels(translatedRect);
buffer = new DataBufferInt(pixels, pixels.length);
bandmasks[0] = screenCapCM.getRedMask();
bandmasks[1] = screenCapCM.getGreenMask();
bandmasks[2] = screenCapCM.getBlueMask();
raster = Raster.createPackedRaster(buffer, translatedRect.width, translatedRect.height, translatedRect.width, bandmasks, null);
image = new BufferedImage(screenCapCM, raster, false, null);
return image;
}
Creates an image containing pixels read from the screen. This image does
not include the mouse cursor. |
public synchronized void delay(int ms) {
checkDelayArgument(ms);
try {
Thread.sleep(ms);
} catch(InterruptedException ite) {
ite.printStackTrace();
}
}
Sleeps for the specified time.
To catch any InterruptedExceptions that occur,
Thread.sleep() may be used instead. |
public synchronized int getAutoDelay() {
return autoDelay;
}
Returns the number of milliseconds this Robot sleeps after generating an event. |
public synchronized Color getPixelColor(int x,
int y) {
Color color = new Color(peer.getRGBPixel(gdLoc.x + x, gdLoc.y + y));
return color;
}
Returns the color of a pixel at the given screen coordinates. |
public synchronized boolean isAutoWaitForIdle() {
return isAutoWaitForIdle;
}
Returns whether this Robot automatically invokes waitForIdle
after generating an event. |
public synchronized void keyPress(int keycode) {
checkKeycodeArgument(keycode);
peer.keyPress(keycode);
afterEvent();
}
Presses a given key. The key should be released using the
keyRelease method.
Key codes that have more than one physical key associated with them
(e.g. KeyEvent.VK_SHIFT could mean either the
left or right shift key) will map to the left key. |
public synchronized void keyRelease(int keycode) {
checkKeycodeArgument(keycode);
peer.keyRelease(keycode);
afterEvent();
}
Releases a given key.
Key codes that have more than one physical key associated with them
(e.g. KeyEvent.VK_SHIFT could mean either the
left or right shift key) will map to the left key. |
public synchronized void mouseMove(int x,
int y) {
peer.mouseMove(gdLoc.x + x, gdLoc.y + y);
afterEvent();
}
Moves mouse pointer to given screen coordinates. |
public synchronized void mousePress(int buttons) {
checkButtonsArgument(buttons);
peer.mousePress(buttons);
afterEvent();
}
Presses one or more mouse buttons. The mouse buttons should
be released using the mouseRelease method. |
public synchronized void mouseRelease(int buttons) {
checkButtonsArgument(buttons);
peer.mouseRelease(buttons);
afterEvent();
}
Releases one or more mouse buttons. |
public synchronized void mouseWheel(int wheelAmt) {
peer.mouseWheel(wheelAmt);
afterEvent();
}
Rotates the scroll wheel on wheel-equipped mice. |
public synchronized void setAutoDelay(int ms) {
checkDelayArgument(ms);
autoDelay = ms;
}
Sets the number of milliseconds this Robot sleeps after generating an event. |
public synchronized void setAutoWaitForIdle(boolean isOn) {
isAutoWaitForIdle = isOn;
}
Sets whether this Robot automatically invokes waitForIdle
after generating an event. |
public synchronized String toString() {
String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle();
return getClass().getName() + "[ " + params + " ]";
}
Returns a string representation of this Robot. |
public synchronized void waitForIdle() {
checkNotDispatchThread();
// post a dummy event to the queue so we know when
// all the events before it have been processed
try {
SunToolkit.flushPendingEvents();
EventQueue.invokeAndWait( new Runnable() {
public void run() {
// dummy implementation
}
} );
} catch(InterruptedException ite) {
System.err.println("Robot.waitForIdle, non-fatal exception caught:");
ite.printStackTrace();
} catch(InvocationTargetException ine) {
System.err.println("Robot.waitForIdle, non-fatal exception caught:");
ine.printStackTrace();
}
}
Waits until all events currently on the event queue have been processed. |