Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is a Robot Class in Java?

Last updated on Apr 28,2020 3.3K Views

6 / 11 Blog from Objects and Classes

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:

What is Robot Class in Java?

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.

Java Robot Class Implementation

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:
Robot class in Java - Edureka

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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is a Robot Class in Java?

edureka.co