- java.lang.Object
- 
- javafx.scene.robot.Robot
 
- 
 public final class Robot extends Object ARobotis used for simulating user interaction such as typing keys on the keyboard and using the mouse as well as capturing graphical information without requiring aSceneinstance. Robot objects must be constructed and used on the JavaFX Application Thread.- Since:
- 11
 
- 
- 
Constructor SummaryConstructors Constructor Description Robot()Constructs a newRobotthat can be used for simulating user interactions.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description Point2DgetMousePosition()Returns the current mouse (x,y) screen coordinates as aPoint2D.doublegetMouseX()Returns the current mousexposition in screen coordinates.doublegetMouseY()Returns the current mouseyposition in screen coordinates.ColorgetPixelColor(double x, double y)Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation.ColorgetPixelColor(Point2D location)Returns theColorof the pixel at the screen coordinates relative to the primary screen specified bylocation.WritableImagegetScreenCapture(WritableImage image, double x, double y, double width, double height)Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.WritableImagegetScreenCapture(WritableImage image, double x, double y, double width, double height, boolean scaleToFit)Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.WritableImagegetScreenCapture(WritableImage image, Rectangle2D region)Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.WritableImagegetScreenCapture(WritableImage image, Rectangle2D region, boolean scaleToFit)Returns aWritableImagecontaining the specified rectangular area relative to the primary screen.voidkeyPress(KeyCode keyCode)Presses the specifiedKeyCodekey.voidkeyRelease(KeyCode keyCode)Releases the specifiedKeyCodekey.voidkeyType(KeyCode keyCode)Types 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.voidmouseMove(Point2D location)Moves 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 Detail- 
Robotpublic 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 the- FXPermission- "createRobot"permission.
 
 
- 
 - 
Method Detail- 
keyPresspublic void keyPress(KeyCode keyCode) 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.
 
 - 
keyReleasepublic void keyRelease(KeyCode keyCode) 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.
 
 - 
keyTypepublic void keyType(KeyCode keyCode) 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.
 
 - 
getMouseXpublic 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.
 
 - 
getMouseYpublic 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.
 
 - 
getMousePositionpublic Point2D 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.
 
 - 
mouseMovepublic 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 to
- y- screen coordinate y to move the mouse to
- Throws:
- IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
 
 - 
mouseMovepublic final void mouseMove(Point2D location) 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.
 
 - 
mousePresspublic void mousePress(MouseButton... buttons) 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.
 
 - 
mouseReleasepublic void mouseRelease(MouseButton... buttons) 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.
 
 - 
mouseClickpublic void mouseClick(MouseButton... buttons) 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.
 
 - 
mouseWheelpublic 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.
 
 - 
getPixelColorpublic Color getPixelColor(double x, double y) 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 from
- y- 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.
 
 - 
getPixelColorpublic Color getPixelColor(Point2D location) 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 given- locationis null
- IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
 
 - 
getScreenCapturepublic 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 a- WritableImagethat will be used to place the screen capture in
- x- the starting x-position of the rectangular area to capture
- y- the starting y-position of the rectangular area to capture
- width- the width of the rectangular area to capture
- height- the height of the rectangular area to capture
- scaleToFit- If true, the returned- Imagewill 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.
 
 - 
getScreenCapturepublic 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 a- WritableImagethat will be used to place the screen capture in
- x- the starting x-position of the rectangular area to capture
- y- the starting y-position of the rectangular area to capture
- width- the width of the rectangular area to capture
- height- 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.
 
 - 
getScreenCapturepublic WritableImage getScreenCapture(WritableImage image, Rectangle2D region) 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 a- WritableImagethat will be used to place the screen capture in
- region- 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.
 
 - 
getScreenCapturepublic WritableImage getScreenCapture(WritableImage image, Rectangle2D region, 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 a- WritableImagethat will be used to place the screen capture in
- region- the rectangular area of the screen to capture
- scaleToFit- if true, the returned- Imagewill 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.
 
 
- 
 
-