This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.awt
class Robot

java.lang.Object extended by java.awt.Robot

Most common way to construct:

Robot robot = new Robot();

Based on 185 examples


public class Robot
extends Object

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.


Constructor Summary

          Constructs a Robot object in the coordinate system of the primary screen.

          Creates a Robot for the given screen device.
 
Method Summary
 BufferedImage

          Creates an image containing pixels read from the screen.
 void
delay(int ms)

          Sleeps for the specified time.
 int

          Returns the number of milliseconds this Robot sleeps after generating an event.
 Color
getPixelColor(int x, int y)

          Returns the color of a pixel at the given screen coordinates.
 boolean

          Returns whether this Robot automatically invokes waitForIdle after generating an event.
 void
keyPress(int keycode)

          Presses a given key.
 void
keyRelease(int keycode)

          Releases a given key.
 void
mouseMove(int x, int y)

          Moves mouse pointer to given screen coordinates.
 void
mousePress(int buttons)

          Presses one or more mouse buttons.
 void
mouseRelease(int buttons)

          Releases one or more mouse buttons.
 void
mouseWheel(int wheelAmt)

          Rotates the scroll wheel on wheel-equipped mice.
 void
setAutoDelay(int ms)

          Sets the number of milliseconds this Robot sleeps after generating an event.
 void
setAutoWaitForIdle(boolean isOn)

          Sets whether this Robot automatically invokes waitForIdle after generating an event.
 String

          Returns a string representation of this Robot.
 void

          Waits until all events currently on the event queue have been processed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Robot

public Robot()
      throws AWTException
Constructs a Robot object in the coordinate system of the primary screen.

Throws:
AWTException - if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true

Robot

public Robot(GraphicsDevice screen)
      throws AWTException
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: This constructor is meant for the latter case.

If screen devices are reconfigured such that the coordinate system is affected, the behavior of existing Robot objects is undefined.

Parameters:
screen - A screen GraphicsDevice indicating the coordinate system the Robot will operate in.
Throws:
AWTException - if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true.
Method Detail

createScreenCapture

public synchronized BufferedImage createScreenCapture(Rectangle screenRect)
Creates an image containing pixels read from the screen. This image does not include the mouse cursor.

Parameters:
screenRect - Rect to capture in screen coordinates
Returns:
The captured image

delay

public synchronized void delay(int ms)
Sleeps for the specified time. To catch any InterruptedExceptions that occur, Thread.sleep() may be used instead.

Parameters:
ms - time to sleep in milliseconds

getAutoDelay

public synchronized int getAutoDelay()
Returns the number of milliseconds this Robot sleeps after generating an event.


getPixelColor

public synchronized Color getPixelColor(int x,
                                        int y)
Returns the color of a pixel at the given screen coordinates.

Parameters:
x - X position of pixel
y - Y position of pixel
Returns:
Color of the pixel

isAutoWaitForIdle

public synchronized boolean isAutoWaitForIdle()
Returns whether this Robot automatically invokes waitForIdle after generating an event.

Returns:
Whether waitForIdle is automatically called

keyPress

public synchronized void keyPress(int keycode)
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.

Parameters:
keycode - Key to press (e.g. KeyEvent.VK_A)

keyRelease

public synchronized void keyRelease(int keycode)
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.

Parameters:
keycode - Key to release (e.g. KeyEvent.VK_A)

mouseMove

public synchronized void mouseMove(int x,
                                   int y)
Moves mouse pointer to given screen coordinates.

Parameters:
x - X position
y - Y position

mousePress

public synchronized void mousePress(int buttons)
Presses one or more mouse buttons. The mouse buttons should be released using the mouseRelease method.

Parameters:
buttons - the Button mask; a combination of one or more of these flags:
  • InputEvent.BUTTON1_MASK
  • InputEvent.BUTTON2_MASK
  • InputEvent.BUTTON3_MASK

mouseRelease

public synchronized void mouseRelease(int buttons)
Releases one or more mouse buttons.

Parameters:
buttons - the Button mask; a combination of one or more of these flags:
  • InputEvent.BUTTON1_MASK
  • InputEvent.BUTTON2_MASK
  • InputEvent.BUTTON3_MASK

mouseWheel

public synchronized void mouseWheel(int wheelAmt)
Rotates the scroll wheel on wheel-equipped mice.

Parameters:
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.

setAutoDelay

public synchronized void setAutoDelay(int ms)
Sets the number of milliseconds this Robot sleeps after generating an event.

Parameters:
ms

setAutoWaitForIdle

public synchronized void setAutoWaitForIdle(boolean isOn)
Sets whether this Robot automatically invokes waitForIdle after generating an event.

Parameters:
isOn - Whether waitForIdle is automatically invoked

toString

public synchronized String toString()
Returns a string representation of this Robot.

Overrides:
toString in class Object
Returns:
the string representation.

waitForIdle

public synchronized void waitForIdle()
Waits until all events currently on the event queue have been processed.



This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.