Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaAutomate mouse events using Java Robot Class

Automate mouse events using Java Robot Class

Robot is part of java.awt package . Robot class is basically used to generate native system input events for the purposes of self- running demos, test automation and other applications where control over mouse and keyboard is used. 
Robot class generates events that can be used to control mouse, keyboard and can be used to take screenshots of the screen. In this article, we will implement Java Robot to move or drag the mouse to specified location. 
Methods used : 
 

  1. mouseMove(int x, int y) : move the mouse to a specified location of screen
  2. keyPress(int k) : presses a given key with a specified keycode
  3. keyRelease(int k) : releases a given key with a specified keycode
  4. mousePress(int b) : presses one or more mouse buttons.
  5. mouseRelease(int b) : Releases one or more mouse buttons.

Program 1 : Java program to move a mouse from the initial location to a specified location 
 

Java




// Java program to move a mouse from the initial
// location to a specified location
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
class robomouse extends Frame implements ActionListener {
    // Frame
    static JFrame f;
 
    // textField
    static TextField x, y;
 
    // default constructor
    robomouse()
    {
    }
 
    // main function
    public static void main(String args[])
    {
        // object of class
        robomouse rm = new robomouse();
 
        // create a frame
        f = new JFrame("robomouse");
 
        // set the frame to close on exit
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create textfield
        x = new TextField(7);
        y = new TextField(7);
 
        // create a button
        Button b = new Button("OK");
 
        // add actionListener
        b.addActionListener(rm);
 
        // create a panel
        Panel p = new Panel();
 
        // add items to panel
        p.add(x);
        p.add(y);
        p.add(b);
 
        f.add(p);
 
        // setsize of frame
        f.setSize(300, 300);
 
        f.show();
    }
 
    // if button is pressed
    public void actionPerformed(ActionEvent e)
    {
        try {
            Robot r = new Robot();
            int xi1, yi1, xi, yi;
 
            // get initial location
            Point p = MouseInfo.getPointerInfo().getLocation();
            xi = p.x;
            yi = p.y;
 
            // get x and y points
            xi1 = Integer.parseInt(x.getText());
            yi1 = Integer.parseInt(y.getText());
            int i = xi, j = yi;
 
            // slowly move the mouse to defined location
            while (i != xi1 || j != yi1) {
                // move the mouse to the other point
                r.mouseMove(i, j);
 
                if (i < xi1)
                    i++;
                if (j < yi1)
                    j++;
 
                if (i > xi1)
                    i--;
                if (j > yi1)
                    j--;
 
                // wait
                Thread.sleep(30);
            }
        }
        catch (Exception evt) {
            System.err.println(evt.getMessage());
        }
    }
}


Output : 
 

Program 2 : Java program to drag a mouse from the one location to another location 
 

Java




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class robomouse1 extends Frame implements ActionListener {
    // Frame
    static JFrame f;
 
    // textField
    static TextField x, y, x1, y1;
 
    // default constructor
    robomouse1()
    {
    }
 
    // main function
    public static void main(String args[])
    {
        // object of class
        robomouse1 rm = new robomouse1();
 
        // create a frame
        f = new JFrame("robomouse");
 
        // set the frame to close on exit
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create textfield
        x = new TextField(7);
        y = new TextField(7);
 
        x1 = new TextField(7);
        y1 = new TextField(7);
 
        // create a button
        Button b = new Button("OK");
 
        // add actionListener
        b.addActionListener(rm);
 
        // create a panel
        Panel p = new Panel();
 
        // create labels
        Label l, l1;
 
        l = new Label("from");
        l1 = new Label("to");
 
        // add items to panel
        p.add(l);
        p.add(x);
        p.add(y);
        p.add(l1);
        p.add(x1);
        p.add(y1);
        p.add(b);
 
        f.add(p);
 
        // setsize of frame
        f.setSize(600, 300);
 
        f.show();
    }
 
    // if button is pressed
    public void actionPerformed(ActionEvent e)
    {
        try {
            Robot r = new Robot();
            int xi, yi, xi1, yi1;
 
            // get x and y points
            xi = Integer.parseInt(x.getText());
            yi = Integer.parseInt(y.getText());
            xi1 = Integer.parseInt(x1.getText());
            yi1 = Integer.parseInt(y1.getText());
 
            // move the mouse to that point
            r.mouseMove(xi, yi);
 
            // press the mouse
            r.mousePress(InputEvent.BUTTON1_MASK);
 
            int i = xi, j = yi;
 
            // slowly drag the mouse to defined location
            while (i < xi1 || j < yi1) {
                // move the mouse to the other point
                r.mouseMove(i, j);
 
                if (i < xi1)
                    i++;
                if (j < yi1)
                    j++;
 
                // wait
                Thread.sleep(30);
            }
 
            // wait
            Thread.sleep(4000);
 
            // press the mouse
            r.mouseRelease(InputEvent.BUTTON1_MASK);
        }
        catch (Exception evt) {
            System.err.println(evt.getMessage());
        }
    }
}


Output : 
 

Note: the code might not run in an online compiler please use an offline IDE.
 

RELATED ARTICLES

Most Popular

Recent Comments