Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Robot class in Java was introduced as a feature in JDK 1.3. It can be used to trigger the input events like mouse move, mouse click, keypress, etc. In this blog on Java Robot Class, we will learn all about the robot class, in the following order:
Java.awt.Robot class is used to take control of keyboard and mouse and conduct different types of operations related to mouse and keyboard through Java code. The Robot class is generally used for test automation.
The below code will be controlling the keyboard and mouse using the Robot Class.
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.*; public class robotic { public static void main(String[] args) throws IOException, AWTException, InterruptedException { String command = "wordpad.exe"; Runtime run = Runtime.getRuntime(); run.exec(command); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } // Create an instance of Robot class Robot myrobot = new Robot(); // keypress will make the virtual keyboard press the parsed keys with a time gap of 300 sec myrobot.keyPress(KeyEvent.VK_L); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_E); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_A); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_R); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_N); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_SPACE); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_W); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_I); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_T); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_H); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_SPACE); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_E); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_D); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_U); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_R); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_E); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_K); Thread.sleep(300); myrobot.keyPress(KeyEvent.VK_A); Thread.sleep(300); } }
Output:
The above program will open Wordpad.exe and type the string “LEARN WITH EDUREKA”, accessing the keyboard using robot class. The below output will be displayed:
With this, we come to an end of this blog on “Java robot class”. If you wish to learn more about Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 12th October,2024 12th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co