public final class Robot
extends java.lang.Object
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 javafx.scene.Scene
instance. Robot objects must be constructed and used on the JavaFX
Application Thread.Modifier and Type | Field and Description |
---|---|
private GlassRobot |
peer |
Constructor and Description |
---|
Robot()
Constructs a new
Robot that can be used for simulating user
interactions. |
Modifier and Type | Method and Description |
---|---|
Point2D |
getMousePosition()
Returns the current mouse (x,y) screen coordinates as a
Point2D . |
double |
getMouseX()
Returns the current mouse
x position in screen coordinates. |
double |
getMouseY()
Returns the current mouse
y position in screen coordinates. |
Color |
getPixelColor(double x,
double y)
Returns the
Color of the pixel at the screen coordinates relative to the
primary screen specified by location . |
Color |
getPixelColor(Point2D location)
Returns the
Color of the pixel at the screen coordinates relative to the
primary screen specified by location . |
WritableImage |
getScreenCapture(WritableImage image,
double x,
double y,
double width,
double height)
Returns a
WritableImage containing the specified rectangular area relative
to the primary screen. |
WritableImage |
getScreenCapture(WritableImage image,
double x,
double y,
double width,
double height,
boolean scaleToFit)
Returns a
WritableImage containing the specified rectangular area relative
to the primary screen. |
WritableImage |
getScreenCapture(WritableImage image,
Rectangle2D region)
Returns a
WritableImage containing the specified rectangular area relative
to the primary screen. |
WritableImage |
getScreenCapture(WritableImage image,
Rectangle2D region,
boolean scaleToFit)
Returns a
WritableImage containing the specified rectangular area relative to
the primary screen. |
void |
keyPress(KeyCode keyCode)
Presses the specified
KeyCode key. |
void |
keyRelease(KeyCode keyCode)
Releases the specified
KeyCode key. |
void |
keyType(KeyCode keyCode)
Types the specified
KeyCode key. |
void |
mouseClick(MouseButton... buttons)
Clicks the specified
MouseButton s. |
void |
mouseMove(double x,
double y)
Moves the mouse to the specified (x,y) screen coordinates relative to
the primary screen.
|
void |
mouseMove(Point2D location)
Moves the mouse to the (x,y) screen coordinates, relative to the primary
screen, specified by the given
location . |
void |
mousePress(MouseButton... buttons)
Presses the specified
MouseButton s. |
void |
mouseRelease(MouseButton... buttons)
Releases the specified
MouseButton s. |
void |
mouseWheel(int wheelAmt)
Scrolls the mouse wheel by the specified amount of wheel clicks.
|
public Robot()
Robot
that can be used for simulating user
interactions. If a security manager is present, the application must
have the javafx.util.FXPermission
"createRobot"
permission
in order to construct a Robot
object.java.lang.IllegalStateException
- if this object is constructed on a thread
other than the JavaFX Application Thread.java.lang.SecurityException
- if a security manager exists and the application
does not have the javafx.util.FXPermission
"createRobot"
permission.public void keyPress(KeyCode keyCode)
KeyCode
key.keyCode
- the key to pressjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if keyCode is null.public void keyRelease(KeyCode keyCode)
KeyCode
key.keyCode
- the key to releasejava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if keyCode is null.public void keyType(KeyCode keyCode)
KeyCode
key.keyCode
- the key to typejava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if keyCode is null.public double getMouseX()
x
position in screen coordinates.x
positionjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public double getMouseY()
y
position in screen coordinates.y
positionjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public Point2D getMousePosition()
Point2D
.java.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public void mouseMove(double x, double y)
x
- screen coordinate x to move the mouse toy
- screen coordinate y to move the mouse tojava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public final void mouseMove(Point2D location)
location
.location
- the (x,y) coordinates to move the mouse tojava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if location is null.public void mousePress(MouseButton... buttons)
MouseButton
s.buttons
- the mouse buttons to pressjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if buttons is null.public void mouseRelease(MouseButton... buttons)
MouseButton
s.buttons
- the mouse buttons to releasejava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if buttons is null.public void mouseClick(MouseButton... buttons)
MouseButton
s.buttons
- the mouse buttons to clickjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if buttons is null.public void mouseWheel(int wheelAmt)
wheelAmt
scrolls the wheel towards the user (down) whereas negative
amounts scrolls the wheel away from the user (up).wheelAmt
- the (signed) amount of clicks to scroll the wheeljava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public Color getPixelColor(double x, double y)
Color
of the pixel at the screen coordinates relative to the
primary screen specified by location
. Regardless of the scale of the screen
(javafx.stage.Screen#getOutputScaleX()
, javafx.stage.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.x
- the x coordinate to get the pixel color fromy
- the y coordinate to get the pixel color fromjava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public Color getPixelColor(Point2D location)
Color
of the pixel at the screen coordinates relative to the
primary screen specified by location
. Regardless of the scale of the screen
(javafx.stage.Screen#getOutputScaleX()
, javafx.stage.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.location
- the (x,y) coordinates to get the pixel color fromjava.lang.NullPointerException
- if the given location
is nulljava.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public WritableImage getScreenCapture(WritableImage image, double x, double y, double width, double height, boolean scaleToFit)
WritableImage
containing the specified rectangular area relative
to the primary screen. If the given image
is null, or if the given
image
is not the required size, a new WritableImage
will be created
and returned. Otherwise, the given image
is re-used.
If the scaleToFit
argument is false, the returned
Image
object dimensions may differ from the requested width
and height
depending 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 an Image
with dimensions (20x20). Calling code should
use the returned images's Image#getWidth()
and Image#getHeight()
methods to determine the actual image size.
If scaleToFit
is true, the returned Image
is 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.
image
- either null or a WritableImage
that 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 returned Image
will 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.region
as a WritableImage
java.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public WritableImage getScreenCapture(WritableImage image, double x, double y, double width, double height)
WritableImage
containing the specified rectangular area relative
to the primary screen. If the given image
is null, or if the given
image
is not the required size, a new WritableImage
will be created
and returned. Otherwise, the given image
is re-used.image
- either null or a WritableImage
that 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 captureregion
as a WritableImage
java.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.public WritableImage getScreenCapture(WritableImage image, Rectangle2D region)
WritableImage
containing the specified rectangular area relative
to the primary screen. If the given image
is null, or if the given
image
is not the required size, a new WritableImage
will be created
and returned. Otherwise, the given image
is re-used.image
- either null or a WritableImage
that will
be used to place the screen capture inregion
- the rectangular area of the screen to captureregion
as a WritableImage
java.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if region is null.public WritableImage getScreenCapture(WritableImage image, Rectangle2D region, boolean scaleToFit)
WritableImage
containing the specified rectangular area relative to
the primary screen. If the given image
is null, or if the given
image
is not the required size, a new WritableImage
will be created
and returned. Otherwise, the given image
is re-used.
If the scaleToFit
argument is false, the returned
Image
object dimensions may differ from the requested width
and height
depending 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 an Image
with dimensions (20x20). Calling code should
use the returned images's Image#getWidth()
and Image#getHeight()
methods to determine the actual image size.
If scaleToFit
is true, the returned Image
is 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.
image
- either null or a WritableImage
that will
be used to place the screen capture inregion
- the rectangular area of the screen to capturescaleToFit
- if true, the returned Image
will 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.region
as a WritableImage
java.lang.IllegalStateException
- if this method is called on a thread
other than the JavaFX Application Thread.java.lang.NullPointerException
- if region is null.