• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Robot Class in Java | keyPress + mouseMove

September 25, 2018 by javainterviewpoint Leave a Comment

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

Robot Class in Java

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.

Filed Under: Core Java, Java Tagged With: Java Robot keyPress, Java Robot mouseMove, keyPress, mouseMove, Robot Class, Robot Class in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·