java.lang.Object | |
↳ | java.awt.Robot |
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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a Robot object in the coordinate system of the primary screen.
| |||||||||||
Creates a Robot for the given screen device.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates an image containing pixels read from the screen.
| |||||||||||
Sleeps for the specified time.
| |||||||||||
Returns the number of milliseconds this Robot sleeps after generating an event.
| |||||||||||
Returns the color of a pixel at the given screen coordinates.
| |||||||||||
Returns whether this Robot automatically invokes
waitForIdle
after generating an event. | |||||||||||
Presses a given key.
| |||||||||||
Releases a given key.
| |||||||||||
Moves mouse pointer to given screen coordinates.
| |||||||||||
Presses one or more mouse buttons.
| |||||||||||
Releases one or more mouse buttons.
| |||||||||||
Rotates the scroll wheel on wheel-equipped mice.
| |||||||||||
Sets the number of milliseconds this Robot sleeps after generating an event.
| |||||||||||
Sets whether this Robot automatically invokes
waitForIdle
after generating an event. | |||||||||||
Returns a string representation of this Robot.
| |||||||||||
Waits until all events currently on the event queue have been processed.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructs a Robot object in the coordinate system of the primary screen.
AWTException | if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true |
---|---|
SecurityException | if createRobot permission is not granted |
Creates a Robot for the given screen device. Coordinates passed to Robot method calls like mouseMove and createScreenCapture will be interpreted as being in the same coordinate system as the specified screen. Note that depending on the platform configuration, multiple screens may either:
If screen devices are reconfigured such that the coordinate system is affected, the behavior of existing Robot objects is undefined.
screen | A screen GraphicsDevice indicating the coordinate system the Robot will operate in. |
---|
AWTException | if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true. |
---|---|
IllegalArgumentException | if screen is not a screen
GraphicsDevice. |
SecurityException | if createRobot permission is not granted |
Creates an image containing pixels read from the screen. This image does not include the mouse cursor.
screenRect | Rect to capture in screen coordinates |
---|
IllegalArgumentException | if screenRect width and height are not greater than zero |
---|---|
SecurityException | if readDisplayPixels permission is not granted |
Sleeps for the specified time.
To catch any InterruptedException
s that occur,
Thread.sleep()
may be used instead.
ms | time to sleep in milliseconds |
---|
IllegalArgumentException | if ms is not between 0 and 60,000 milliseconds inclusive |
---|
Returns the number of milliseconds this Robot sleeps after generating an event.
Returns the color of a pixel at the given screen coordinates.
x | X position of pixel |
---|---|
y | Y position of pixel |
Returns whether this Robot automatically invokes waitForIdle
after generating an event.
waitForIdle
is automatically called
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.
keycode | Key to press (e.g. KeyEvent.VK_A ) |
---|
IllegalArgumentException | if keycode is not
a valid key |
---|
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.
keycode | Key to release (e.g. KeyEvent.VK_A ) |
---|
IllegalArgumentException | if keycode is not a
valid key |
---|
Moves mouse pointer to given screen coordinates.
x | X position |
---|---|
y | Y position |
Presses one or more mouse buttons. The mouse buttons should
be released using the mouseRelease
method.
buttons | the Button mask; a combination of one or more
of these flags:
|
---|
IllegalArgumentException | if the button mask is not a valid combination |
---|
Releases one or more mouse buttons.
buttons | the Button mask; a combination of one or more
of these flags:
|
---|
IllegalArgumentException | if the button mask is not a valid combination |
---|
Rotates the scroll wheel on wheel-equipped mice.
wheelAmt | number of "notches" to move the mouse wheel Negative values indicate movement up/away from the user, positive values indicate movement down/towards the user. |
---|
Sets the number of milliseconds this Robot sleeps after generating an event.
IllegalArgumentException | If ms is not between 0 and 60,000 milliseconds inclusive
|
---|
Sets whether this Robot automatically invokes waitForIdle
after generating an event.
isOn | Whether waitForIdle is automatically invoked
|
---|
Returns a string representation of this Robot.
Waits until all events currently on the event queue have been processed.
IllegalThreadStateException | if called on the AWT event dispatching thread |
---|