Robot is used for simulating user interaction such as
typing keys on the keyboard and using the mouse as well as capturing
graphical information without requiring a Scene
instance. Robot objects must be constructed and used on the JavaFX
Application Thread.- Since:
- 11
-
Constructor Summary
ConstructorsConstructorDescriptionRobot()Constructs a newRobotthat can be used for simulating user interactions. -
Method Summary
Modifier and TypeMethodDescriptionReturns the current mouse (x,y) screen coordinates as aPoint2D.doubleReturns the current mousexposition in screen coordinates.doubleReturns the current mouseyposition in screen coordinates.getPixelColor(double x, double y) Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation.getPixelColor(Point2D location) Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation.getScreenCapture(WritableImage image, double x, double y, double width, double height) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.getScreenCapture(WritableImage image, double x, double y, double width, double height, boolean scaleToFit) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.getScreenCapture(WritableImage image, Rectangle2D region) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.getScreenCapture(WritableImage image, Rectangle2D region, boolean scaleToFit) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.voidPresses the specifiedKeyCodekey.voidkeyRelease(KeyCode keyCode) Releases the specifiedKeyCodekey.voidTypes the specifiedKeyCodekey.voidmouseClick(MouseButton... buttons) Clicks the specifiedMouseButtons.voidmouseMove(double x, double y) Moves the mouse to the specified (x,y) screen coordinates relative to the primary screen.final voidMoves the mouse to the (x,y) screen coordinates, relative to the primary screen, specified by the givenlocation.voidmousePress(MouseButton... buttons) Presses the specifiedMouseButtons.voidmouseRelease(MouseButton... buttons) Releases the specifiedMouseButtons.voidmouseWheel(int wheelAmt) Scrolls the mouse wheel by the specified amount of wheel clicks.
-
Constructor Details
-
Robot
public Robot()Constructs a newRobotthat can be used for simulating user interactions. If a security manager is present, the application must have theFXPermission"createRobot"permission in order to construct aRobotobject.- Throws:
IllegalStateException- if this object is constructed on a thread other than the JavaFX Application Thread.SecurityException- if a security manager exists and the application does not have theFXPermission"createRobot"permission.
-
-
Method Details
-
keyPress
Presses the specifiedKeyCodekey.- Parameters:
keyCode- the key to press- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if keyCode is null.
-
keyRelease
Releases the specifiedKeyCodekey.- Parameters:
keyCode- the key to release- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if keyCode is null.
-
keyType
Types the specifiedKeyCodekey.- Implementation Requirements:
- This is a convenience method that is equivalent to calling
keyPress(KeyCode)followed bykeyRelease(KeyCode). - Parameters:
keyCode- the key to type- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if keyCode is null.
-
getMouseX
public double getMouseX()Returns the current mousexposition in screen coordinates.- Returns:
- the current mouse
xposition - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getMouseY
public double getMouseY()Returns the current mouseyposition in screen coordinates.- Returns:
- the current mouse
yposition - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getMousePosition
Returns the current mouse (x,y) screen coordinates as aPoint2D.- Returns:
- the current mouse (x,y) screen coordinates
- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
mouseMove
public void mouseMove(double x, double y) Moves the mouse to the specified (x,y) screen coordinates relative to the primary screen.- Parameters:
x- screen coordinate x to move the mouse toy- screen coordinate y to move the mouse to- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
mouseMove
Moves the mouse to the (x,y) screen coordinates, relative to the primary screen, specified by the givenlocation.- Parameters:
location- the (x,y) coordinates to move the mouse to- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if location is null.
-
mousePress
Presses the specifiedMouseButtons.- Parameters:
buttons- the mouse buttons to press- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if buttons is null.
-
mouseRelease
Releases the specifiedMouseButtons.- Parameters:
buttons- the mouse buttons to release- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if buttons is null.
-
mouseClick
Clicks the specifiedMouseButtons.- Implementation Requirements:
- This is a convenience method that is equivalent to calling
mousePress(MouseButton...)followed bymouseRelease(MouseButton...). - Parameters:
buttons- the mouse buttons to click- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if buttons is null.
-
mouseWheel
public void mouseWheel(int wheelAmt) Scrolls the mouse wheel by the specified amount of wheel clicks. A positivewheelAmtscrolls the wheel towards the user (down) whereas negative amounts scrolls the wheel away from the user (up).- Parameters:
wheelAmt- the (signed) amount of clicks to scroll the wheel- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getPixelColor
Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation. Regardless of the scale of the screen (Screen.getOutputScaleX(),Screen.getOutputScaleY()), this method only samples a single pixel. For example, on a HiDPI screen with output scale 2, the screen unit at the point (x,y) may have 4 pixels. In this case the color returned is the color of the top, left pixel. Color values are not averaged when a screen unit is made up of more than one pixel.- Parameters:
x- the x coordinate to get the pixel color fromy- the y coordinate to get the pixel color from- Returns:
- the pixel color at the specified screen coordinates
- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getPixelColor
Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation. Regardless of the scale of the screen (Screen.getOutputScaleX(),Screen.getOutputScaleY()), this method only samples a single pixel. For example, on a HiDPI screen with output scale 2, the screen unit at the point (x,y) may have 4 pixels. In this case the color returned is the color of the top, left pixel. Color values are not averaged when a screen unit is made up of more than one pixel.- Parameters:
location- the (x,y) coordinates to get the pixel color from- Returns:
- the pixel color at the specified screen coordinates
- Throws:
NullPointerException- if the givenlocationis nullIllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getScreenCapture
public WritableImage getScreenCapture(WritableImage image, double x, double y, double width, double height, boolean scaleToFit) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen. If the givenimageis null, or if the givenimageis not the required size, a newWritableImagewill be created and returned. Otherwise, the givenimageis re-used.If the
scaleToFitargument is false, the returnedImageobject dimensions may differ from the requestedwidthandheightdepending on how many physical pixels the area occupies on the screen. For example, in HiDPI mode on the Mac (aka Retina display) the pixels are doubled, and thus a screen capture of an area of size (10x10) pixels will result in anImagewith dimensions (20x20). Calling code should use the returned images'sImage.getWidth()andImage.getHeight()methods to determine the actual image size.If
scaleToFitis true, the returnedImageis of the requested size. Note that in this case the image will be scaled in order to fit to the requested dimensions if necessary, such as when running on a HiDPI display.- Parameters:
image- either null or aWritableImagethat will be used to place the screen capture inx- the starting x-position of the rectangular area to capturey- the starting y-position of the rectangular area to capturewidth- the width of the rectangular area to captureheight- the height of the rectangular area to capturescaleToFit- If true, the returnedImagewill be scaled to fit the request dimensions (if necessary). Otherwise, the size of the returned image will depend on the output scale (DPI) of the primary screen.- Returns:
- the screen capture of the specified
regionas aWritableImage - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getScreenCapture
public WritableImage getScreenCapture(WritableImage image, double x, double y, double width, double height) Returns aWritableImagecontaining the specified rectangular area relative to the primary screen. If the givenimageis null, or if the givenimageis not the required size, a newWritableImagewill be created and returned. Otherwise, the givenimageis re-used.- Implementation Requirements:
- This method is equivalent to calling
getScreenCapture(x, y, width, height, true), that is, this method scales the image to fit the requested size. - Parameters:
image- either null or aWritableImagethat will be used to place the screen capture inx- the starting x-position of the rectangular area to capturey- the starting y-position of the rectangular area to capturewidth- the width of the rectangular area to captureheight- the height of the rectangular area to capture- Returns:
- the screen capture of the specified
regionas aWritableImage - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
getScreenCapture
Returns aWritableImagecontaining the specified rectangular area relative to the primary screen. If the givenimageis null, or if the givenimageis not the required size, a newWritableImagewill be created and returned. Otherwise, the givenimageis re-used.- Implementation Requirements:
- This method is equivalent to calling
getScreenCapture(image, region, true), that is, this method scales the image to fit the requested size. - Parameters:
image- either null or aWritableImagethat will be used to place the screen capture inregion- the rectangular area of the screen to capture- Returns:
- the screen capture of the specified
regionas aWritableImage - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if region is null.
-
getScreenCapture
Returns aWritableImagecontaining the specified rectangular area relative to the primary screen. If the givenimageis null, or if the givenimageis not the required size, a newWritableImagewill be created and returned. Otherwise, the givenimageis re-used.If the
scaleToFitargument is false, the returnedImageobject dimensions may differ from the requestedwidthandheightdepending on how many physical pixels the area occupies on the screen. For example, in HiDPI mode on the Mac (aka Retina display) the pixels are doubled, and thus a screen capture of an area of size (10x10) pixels will result in anImagewith dimensions (20x20). Calling code should use the returned images'sImage.getWidth()andImage.getHeight()methods to determine the actual image size.If
scaleToFitis true, the returnedImageis of the requested size. Note that in this case the image will be scaled in order to fit to the requested dimensions if necessary, such as when running on a HiDPI display.- Parameters:
image- either null or aWritableImagethat will be used to place the screen capture inregion- the rectangular area of the screen to capturescaleToFit- if true, the returnedImagewill be scaled to fit the request dimensions (if necessary). Otherwise, the size of the returned image will depend on the output scale (DPI) of the primary screen.- Returns:
- the screen capture of the specified
regionas aWritableImage - Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- if region is null.
-