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.
API Note
When autoWaitForIdle() is enabled, mouse and key related methods
cannot be called on the AWT EDT. This is because when autoWaitForIdle()
is enabled, the mouse and key methods implicitly call waitForIdle()
which will throw IllegalThreadStateException when called on the AWT EDT.
In addition, screen capture operations can be lengthy
and delay(long ms) clearly inserts a delay, so these also
should not be called on the EDT. Taken together, this means that as much as possible,
methods on this class should not be called on the EDT.
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.
Platforms and desktop environments may impose restrictions or limitations on the access required to implement all functionality in the Robot class. For example:
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| public static final int | DEFAULT_DELAY
Default delay in milliseconds for mouse
|
| public static final int | DEFAULT_STEP_LENGTH
Default pixel step-length for mouse
|
| private boolean | |
| private static int | |
| private static final int | |
| private RobotPeer | |
| private DirectColorModel |
| Access | Constructor and Description |
|---|---|
| public | |
| public | Robot(GraphicsDevice
A screen GraphicsDevice indicating the coordinate
system the Robot will operate in. screen)Creates a Robot for the given screen device. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| private void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| public void | click(int
The button mask; a combination of one or more mouse button masks. buttons)A convenience method that simulates clicking a mouse button by calling |
| public void | |
| private synchronized BufferedImage[] | |
| public synchronized MultiResolutionImage | Returns: The captured imageRect to capture in screen coordinates screenRect)Creates an image containing pixels read from the screen. |
| public synchronized BufferedImage | Returns: The captured imageRect to capture in screen coordinates screenRect)Creates an image containing pixels read from the screen. |
| public void | |
| public synchronized int | Returns: the delay duration in millisecondsReturns the number of milliseconds this Robot sleeps after generating an event. |
| public synchronized Color | Returns: Color of the pixelX position of pixel x, int Y position of pixel y)Returns the color of a pixel at the given screen coordinates. |
| public void | glide(int
Destination point x coordinate x, int Destination point y coordinate y)A convenience method that moves the mouse in multiple steps from its current location to the destination coordinates. |
| public void | glide(int
Source point x coordinate srcX, int Source point y coordinate srcY, int Destination point x coordinate dstX, int Destination point y coordinate dstY)A convenience method that moves the mouse in multiple steps from source coordinates to the destination coordinates. |
| public void | glide(int
Source point x coordinate srcX, int Source point y coordinate srcY, int Destination point x coordinate destX, int Destination point y coordinate destY, int Preferred length of one step in pixels stepLength, int Delay between steps in milliseconds stepDelay)A convenience method that moves the mouse in multiple
steps from source point to the destination point with a
given |
| private void | |
| private static synchronized void | |
| public synchronized boolean | Returns: WhetherwaitForIdle is automatically calledReturns whether this Robot automatically invokes |
| public synchronized void | |
| public synchronized void | |
| public synchronized void | |
| public synchronized void | mousePress(int
the Button mask; a combination of one or more
mouse button masks.
buttons)
It is allowed to use only a combination of valid values as a
The following standard button masks are also accepted:
InputEvent.BUTTON1_DOWN_MASK,
InputEvent.BUTTON2_DOWN_MASK, InputEvent.BUTTON3_DOWN_MASK instead.
Either extended _DOWN_MASK or old _MASK values
should be used, but both those models should not be mixed.Presses one or more mouse buttons. |
| public synchronized void | mouseRelease(int
the Button mask; a combination of one or more
mouse button masks.
buttons)
It is allowed to use only a combination of valid values as a
The following standard button masks are also accepted:
InputEvent.BUTTON1_DOWN_MASK,
InputEvent.BUTTON2_DOWN_MASK, InputEvent.BUTTON3_DOWN_MASK instead.
Either extended _DOWN_MASK or old _MASK values
should be used, but both those models should not be mixed.Releases one or more mouse buttons. |
| public synchronized void | mouseWheel(int
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. wheelAmt)Rotates the scroll wheel on wheel-equipped mice. |
| public synchronized void | setAutoDelay(int
the delay duration in milliseconds ms)Sets the number of milliseconds this Robot sleeps after generating an event. |
| public synchronized void | setAutoWaitForIdle(boolean
Whether isOn)waitForIdle is automatically invokedSets whether this Robot automatically invokes |
| public synchronized String | Returns: the string representation.Overrides java. Returns a string representation of this Robot. |
| public synchronized void | type(int
Key to type (e.g. keycode)KeyEvent.VK_A)A convenience method that simulates typing a key by calling |
| public synchronized void | type(char
Character to be typed (e.g. c)'a')A convenience method that simulates typing a char by calling |
| public synchronized void | |
| public void | waitForIdle(int
Additional delay length in milliseconds to wait until thread
sync been completed delayValue)A convenience method that calls |
| autoDelay | back to summary |
|---|---|
| private int autoDelay | |
| DEFAULT_DELAY | back to summary |
|---|---|
| public static final int DEFAULT_DELAY Default delay in milliseconds for mouse
| |
| DEFAULT_STEP_LENGTH | back to summary |
|---|---|
| public static final int DEFAULT_STEP_LENGTH Default pixel step-length for mouse
| |
| isAutoWaitForIdle | back to summary |
|---|---|
| private boolean isAutoWaitForIdle | |
| LEGAL_BUTTON_MASK | back to summary |
|---|---|
| private static int LEGAL_BUTTON_MASK | |
| MAX_DELAY | back to summary |
|---|---|
| private static final int MAX_DELAY | |
| peer | back to summary |
|---|---|
| private RobotPeer peer | |
| screenCapCM | back to summary |
|---|---|
| private DirectColorModel screenCapCM | |
| Robot | back to summary |
|---|---|
| public Robot() throws AWTException Constructs a Robot object in the coordinate system of the primary screen.
| |
| Robot | back to summary |
|---|---|
| public Robot(GraphicsDevice screen) throws AWTException Creates a Robot for the given screen device. Coordinates passed to Robot method calls like mouseMove, getPixelColor 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.
| |
| afterEvent | back to summary |
|---|---|
| private void afterEvent() | |
| autoDelay | back to summary |
|---|---|
| private void autoDelay() | |
| autoWaitForIdle | back to summary |
|---|---|
| private void autoWaitForIdle() | |
| checkButtonsArgument | back to summary |
|---|---|
| private static void checkButtonsArgument(int buttons) | |
| checkDelayArgument | back to summary |
|---|---|
| private static void checkDelayArgument(int ms) | |
| checkHeadless | back to summary |
|---|---|
| private static void checkHeadless() throws AWTException Check for headless state and throw | |
| checkIsScreenDevice | back to summary |
|---|---|
| private static void checkIsScreenDevice(GraphicsDevice device) | |
| checkKeycodeArgument | back to summary |
|---|---|
| private static void checkKeycodeArgument(int keycode) | |
| checkNotDispatchThread | back to summary |
|---|---|
| private static void checkNotDispatchThread() | |
| checkValidRect | back to summary |
|---|---|
| private static void checkValidRect(Rectangle rect) | |
| click | back to summary |
|---|---|
| public void click(int buttons) A convenience method that simulates clicking a mouse button by calling
| |
| click | back to summary |
|---|---|
| public void click() A convenience method that clicks mouse button 1.
| |
| createCompatibleImage | back to summary |
|---|---|
| private synchronized BufferedImage[] createCompatibleImage(Rectangle screenRect, boolean isHiDPI) | |
| createMultiResolutionScreenCapture | back to summary |
|---|---|
| public synchronized MultiResolutionImage createMultiResolutionScreenCapture(Rectangle screenRect) Creates an image containing pixels read from the screen.
This method can be used in case there is a scaling transform
from user space to screen (device) space.
Typically this means that the display is a high resolution screen,
although strictly it means any case in which there is such a transform.
Returns a
For a non-scaled display, the
For a high resolution display where there is a scaling transform,
the
Example:
API Note It is recommended to avoid calling this method on the AWT Event Dispatch Thread since screen capture may be a lengthy operation, particularly if acquiring permissions is needed and involves user interaction.
| |
| createScreenCapture | back to summary |
|---|---|
| public synchronized BufferedImage createScreenCapture(Rectangle screenRect) Creates an image containing pixels read from the screen.
If the desktop environment requires that permissions be granted
to capture screen content, and the required permissions are not granted,
then a API Note It is recommended to avoid calling this method on the AWT Event Dispatch Thread since screen capture may be a lengthy operation, particularly if acquiring permissions is needed and involves user interaction.
| |
| delay | back to summary |
|---|---|
| public void delay(int ms) Sleeps for the specified time. If the invoking thread is interrupted while waiting, then it will return immediately with the interrupted status set. If the interrupted status is already set, this method returns immediately with the interrupted status set. API Note It is recommended to avoid calling this method on the AWT Event Dispatch Thread since delay may be a lengthy operation.
| |
| getAutoDelay | back to summary |
|---|---|
| public synchronized int getAutoDelay() Returns the number of milliseconds this Robot sleeps after generating an event.
| |
| getPixelColor | back to summary |
|---|---|
| public synchronized Color getPixelColor(int x, int y) Returns the color of a pixel at the given screen coordinates.
If the desktop environment requires that permissions be granted
to capture screen content, and the required permissions are not granted,
then a API Note It is recommended to avoid calling this method on the AWT Event Dispatch Thread since screen capture may be a lengthy operation, particularly if acquiring permissions is needed and involves user interaction.
| |
| glide | back to summary |
|---|---|
| public void glide(int x, int y) A convenience method that moves the mouse in multiple steps from its current location to the destination coordinates. Implementation Specification Invokes
| |
| glide | back to summary |
|---|---|
| public void glide(int srcX, int srcY, int dstX, int dstY) A convenience method that moves the mouse in multiple steps from source coordinates to the destination coordinates. Implementation Specification Invokes
| |
| glide | back to summary |
|---|---|
| public void glide(int srcX, int srcY, int destX, int destY, int stepLength, int stepDelay) A convenience method that moves the mouse in multiple
steps from source point to the destination point with a
given
| |
| init | back to summary |
|---|---|
| private void init(GraphicsDevice screen) throws AWTException | |
| initLegalButtonMask | back to summary |
|---|---|
private static synchronized void initLegalButtonMask()
| |
| isAutoWaitForIdle | back to summary |
|---|---|
| public synchronized boolean isAutoWaitForIdle() Returns whether this Robot automatically invokes
| |
| keyPress | back to summary |
|---|---|
| public synchronized void keyPress(int keycode) Presses a given key. The key should be released using the
Key codes that have more than one physical key associated with them
(e.g.
| |
| keyRelease | back to summary |
|---|---|
| public synchronized void keyRelease(int keycode) Releases a given key.
Key codes that have more than one physical key associated with them
(e.g.
| |
| mouseMove | back to summary |
|---|---|
| public synchronized void mouseMove(int x, int y) Moves mouse pointer to given screen coordinates. The mouse pointer may not visually move on some platforms, while the subsequent mousePress and mouseRelease can be delivered to the correct location
| |
| mousePress | back to summary |
|---|---|
| public synchronized void mousePress(int buttons) Presses one or more mouse buttons. The mouse buttons should
be released using the
| |
| mouseRelease | back to summary |
|---|---|
| public synchronized void mouseRelease(int buttons) Releases one or more mouse buttons.
| |
| mouseWheel | back to summary |
|---|---|
| public synchronized void mouseWheel(int wheelAmt) Rotates the scroll wheel on wheel-equipped mice.
| |
| setAutoDelay | back to summary |
|---|---|
| public synchronized void setAutoDelay(int ms) Sets the number of milliseconds this Robot sleeps after generating an event.
| |
| setAutoWaitForIdle | back to summary |
|---|---|
| public synchronized void setAutoWaitForIdle(boolean isOn) Sets whether this Robot automatically invokes API Note Setting this to true means you cannot call mouse and key-controlling events on the AWT Event Dispatching Thread
| |
| toString | back to summary |
|---|---|
| public synchronized String toString() Overrides java. Returns a string representation of this Robot. | |
| type | back to summary |
|---|---|
| public synchronized void type(int keycode) A convenience method that simulates typing a key by calling
Key codes that have more than one physical key associated with them
(e.g.
| |
| type | back to summary |
|---|---|
| public synchronized void type(char c) A convenience method that simulates typing a char by calling
| |
| waitForIdle | back to summary |
|---|---|
| public synchronized void waitForIdle() Waits until all events currently on the event queue have been processed.
| |
| waitForIdle | back to summary |
|---|---|
| public void waitForIdle(int delayValue) A convenience method that calls
| |