Javatpoint Logo
Javatpoint Logo

Java Robot

In Java, Robot is a class that belongs to the java.awt package. It also extends the Object class. The class is used to generate native system input events for test automation, self-running demos, and other applications where the control of the mouse and keyboard is required. In other words, we use the Java Robot class to trigger the input events, such as mouse click, keypress, etc. In this section, we will discuss the Java Robot class and its methods. Along with this, we will also create a Java program to control the keyboard and mouse. By using the Robot class we will create a Java program and control the keyboard and mouse.

The primary purpose of the Robot class is to facilitate automated testing of Java platform implementations. In short, the Robot class is used to control the peripherals (mouse and keyboard). It facilitates the three main functionalities, provides control over the mouse and keyboard, and captures the screen also.

Java Robot Class Methods

Method Name Description
createScreenCapture(Rectangle screenRect) The method captures an image from the screen in a rectangular shape.
delay(int ms) It is used to sleep for a specified time.
getAutoDelay() It returns the number of milliseconds this robot sleeps after generating an event.
getPixelColor(int x, int y) It returns the color of the pixel of the specified coordinates.
isAutoWaitForIdle() The method returns whether this Robot automatically invokes the waitForIdle() after generating an event.
keyPress(int keycode) The method presses the specific key.
keyRelease(int keycode) It releases the specified key.
mouseMove(int x, int y) It moves the mouse pointer over the specified coordinates.
mousePress(int buttons) It presses the mouse buttons.
mouseRelease(int buttons) The method releases the mouse buttons.
mouseWheel(int wheelAmt) It scrolls the mouse wheel.
setAutoDelay(int ms) It sets the number of milliseconds this Robot sleeps after generating an event.
setAutoWaitForIdle(boolean isOn) It sets whether this Robot automatically invokes the waitForIdle() method after generating an event.
toString() It returns a string representation of the Robot.
waitForIdle() It waits until all events currently on the event queue have been processed.

The Robot class extends the Object class so the methods of the Object class also inherited in the Robot class. All the above methods throw IllegalArgumentException if the key or mouse click is not correctly specified.

KeyBoardControlExample.java

Output:

Java Robot

When we run the above program, the notepad.exe automatically opens and starts hitting the corresponding keys as arranged in order. After pressing each key there is an interval of 500 milliseconds (ms), you can increase or decrease the delay accordingly. Hence, the string welcome to javatpoint is simulated types on the notepad.

Robot Class Mouse Events

Let's understand how to simulate mouse events using the Java Robot class.

mousePress(int buttons)

The method parses one or more mouse buttons. After pressing the muse button should be released by invoking the mouseRelease(int buttons) method. For mouse events, there are following button masks (that passed in the parameter) are available:

  • BUTTON1_DOWN_MASK: It performs left-click event.
  • BUTTON2_DOWN_MASK: It performs middle button click.
  • BUTTON3_DOWN_MASK: It performs right-button click.

The above values returned by the InputEvent.getMaskForButton(button) method. The method also accepts the following standard button masks:

  • BUTTON1_MASK
  • BUTTON2_MASK
  • BUTTON3_MASK

Syntax:

mouseRelease(int buttons)

It releases the mouse button that is previously clicked. It accepts the same parameter as the method mousePress() method accepts. Suppose, we want to release the right-click then will invoke the robot.mouseRelease(InputEvent BUTTON3_DOWN_MASK) method.

Syntax:

mouseMove(int x, int y)

The method is used to move the mouse pointer to the specified screen position. It parses two parameters x and y that are coordinates. For example, mouseMove(300, 100).

Syntax:

mouseWheel()

The method rotates the mouse wheel. It parses a parameter wheelAmt that is a number of notches to move the wheel. The parameter may have a positive or negative value. The positive value moves the wheel in the down direction (towards the user) and the negative value moves the wheel in the up direction (away from the user).

Syntax:

Let's implement the mouse simulation in a Java program using the Java Robot class.

RobotMouse.java

Output:

Java Robot

To move the pointer, click on the OK button, as we click on the OK button the pointer starts moving to the specified coordinate.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA