Robot class is introduced as a feature in JDK 1.3. Robot Class in Java can be used to trigger the input events such as mouse move, mouse click, key press etc. Robot class can be used to facilitate automation testing or self-running demos where we need to programmatically control the keyboard or mouse.
Robot Class in Java
Let’s take a look into the below code where we will be controlling the keyboard and mouse using the Robot class.
Java Robot keyPress Event
In the below code we will open up the notepad and use the Java Robot class to write the message “hello from robot” in the notepad.
package com.javainterviewpoint; import java.awt.Robot; import java.awt.event.KeyEvent; public class RobotKeyPress { public static void main(String[] args) { try { // Open notepad using Runtime class Runtime runtime = Runtime.getRuntime(); runtime.exec("notepad.exe"); // Sleep time of 1 sec to open notepad Thread.sleep(1000); // Create a new instance for the Robot class Robot robot = new Robot(); // Trigger keypress events robot.keyPress(KeyEvent.VK_H); robot.keyPress(KeyEvent.VK_E); robot.keyPress(KeyEvent.VK_L); robot.keyPress(KeyEvent.VK_L); robot.keyPress(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_SPACE); robot.keyPress(KeyEvent.VK_F); robot.keyPress(KeyEvent.VK_R); robot.keyPress(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_M); robot.keyPress(KeyEvent.VK_SPACE); robot.keyPress(KeyEvent.VK_R); robot.keyPress(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_B); robot.keyPress(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_T); } catch (Exception e) { e.printStackTrace(); } } }
- Create a new instance for the Runtime class. Java uses Runtime class is used to interact with the runtime environment, only one instance of Runtime class is available for one java application.
Runtime runtime = Runtime.getRuntime();
- Call the exec() method on top of the runtime instance, passing notepad.exe as a parameter to it.
runtime.exec("notepad.exe");
- We have added a sleep time of 1000 ms (1 second) in order to wait for the notepad to open
Thread.sleep(1000);
- Create a new instance for the Robot.
Robot robot = new Robot();
- Call the keyPress() method on top of the robot instance, passing KeyEvent
robot.keyPress(KeyEvent.VK_H); robot.keyPress(KeyEvent.VK_E);
Output
Java Robot mouseMove Event
package com.javainterviewpoint; import java.awt.Robot; import java.util.Random; public class RobotMoveMouse { public static void main(String[] args) throws Exception { // Create a new instance for the Robot class Robot robot = new Robot(); Random random = new Random(); while (true) { //Delay of 6 seconds robot.delay(6000); int x = random.nextInt() % 640; int y = random.nextInt() % 480; //Move the mouse pointer robot.mouseMove(x, y); } } }
- Create a new instance for the Robot class.
Robot robot = new Robot();
- We have created an instance of Random class to get the random position of x and y to move the mouse
Random random = new Random();
- We have created a delay of 6 seconds using the delay() method
robot.delay(6000);
- Now call the mouseMove() method on top of robot instance to move the mouse pointer
robot.mouseMove(x, y);
Output
The above code moves the mouse pointer randomly for every 6 seconds.
Leave a Reply