public class Robot extends Object
AWTEvents on the AWT event queue, its use
is not restricted to Java programs. It can be used to
programatically drive any graphical application.
This implementation requires an X server that supports the XTest
extension.| Constructor and Description |
|---|
Robot()
Construct a Robot object that operates on the default screen.
|
Robot(GraphicsDevice screen)
Construct a Robot object that operates on the specified screen.
|
| Modifier and Type | Method and Description |
|---|---|
BufferedImage |
createScreenCapture(Rectangle screenRect)
Create an image containing pixels read from the screen.
|
void |
delay(int ms)
Sleep for a specified length of time.
|
int |
getAutoDelay()
Retrieve the length of time this Robot sleeps after generating an
event.
|
Color |
getPixelColor(int x,
int y)
Return the color of the pixel at the given screen coordinates.
|
boolean |
isAutoWaitForIdle()
Check if this Robot automatically calls
waitForIdle() after
generating an event. |
void |
keyPress(int keycode)
Press a key.
|
void |
keyRelease(int keycode)
Release a key.
|
void |
mouseMove(int x,
int y)
Move the mouse pointer to absolute coordinates (x, y).
|
void |
mousePress(int buttons)
Press one or more mouse buttons.
|
void |
mouseRelease(int buttons)
Release one or more mouse buttons.
|
void |
mouseWheel(int wheelAmt)
Rotate the mouse scroll wheel.
|
void |
setAutoDelay(int ms)
Set the length of time this Robot sleeps after generating an
event.
|
void |
setAutoWaitForIdle(boolean isOn)
Set whether or not this Robot automatically calls
waitForIdle() after generating an event. |
String |
toString()
Return a string representation of this Robot.
|
void |
waitForIdle()
Wait until all events currently on the event queue have been
dispatched.
|
public Robot() throws AWTException
AWTException - if GraphicsEnvironment.isHeadless()
returns true or if the X server does not support the XTest
extensionSecurityException - if createRobot permission is not
grantedpublic Robot(GraphicsDevice screen) throws AWTException
AWTException - if GraphicsEnvironment.isHeadless()
returns true or if the X server does not support the XTest
extensionIllegalArgumentException - if screen is not a screen
GraphicsDeviceSecurityException - if createRobot permission is not
grantedpublic void mouseMove(int x, int y)
x - the destination x coordinatey - the destination y coordinatepublic void mousePress(int buttons)
buttons - the buttons to press; a bitmask of one or more of
these InputEvent fields:
IllegalArgumentException - if the button mask is invalidpublic void mouseRelease(int buttons)
buttons - the buttons to release; a bitmask of one or more
of these InputEvent fields:
IllegalArgumentException - if the button mask is invalidpublic void mouseWheel(int wheelAmt)
wheelAmt - number of steps to rotate mouse wheel. negative
to rotate wheel up (away from the user), positive to rotate wheel
down (toward the user).public void keyPress(int keycode)
keycode - key to press, a KeyEvent VK_ constantIllegalArgumentException - if keycode is not a valid keypublic void keyRelease(int keycode)
keycode - key to release, a KeyEvent VK_
constantIllegalArgumentException - if keycode is not a valid keypublic Color getPixelColor(int x, int y)
x - the x coordinate of the pixely - the y coordinate of the pixel(x, y)public BufferedImage createScreenCapture(Rectangle screenRect)
screenRect - the rectangle of pixels to capture, in screen
coordinatesIllegalArgumentException - if requested width and height
are not both greater than zeroSecurityException - if readDisplayPixels permission is
not grantedpublic boolean isAutoWaitForIdle()
waitForIdle() after
generating an event.public void setAutoWaitForIdle(boolean isOn)
waitForIdle() after generating an event.isOn - true if waitForIdle should be called automaticallypublic int getAutoDelay()
public void setAutoDelay(int ms)
ms - the length of time in millisecondsIllegalArgumentException - if ms is not between 0 and
60,000 milliseconds inclusivepublic void delay(int ms)
ms - the length of time in millisecondsIllegalArgumentException - if ms is not between 0 and
60,000 milliseconds inclusivepublic void waitForIdle()
public String toString()
toString in class ObjectObject.getClass(),
Object.hashCode(),
Class.getName(),
Integer.toHexString(int)