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 :
- mouseMove(int x, int y) : move the mouse to a specified location of screen
- keyPress(int k) : presses a given key with a specified keycode
- keyRelease(int k) : releases a given key with a specified keycode
- mousePress(int b) : presses one or more mouse buttons.
- 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class robomouse extends Frame implements ActionListener {
static JFrame f;
static TextField x, y;
robomouse()
{
}
public static void main(String args[])
{
robomouse rm = new robomouse();
f = new JFrame( "robomouse" );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = new TextField( 7 );
y = new TextField( 7 );
Button b = new Button( "OK" );
b.addActionListener(rm);
Panel p = new Panel();
p.add(x);
p.add(y);
p.add(b);
f.add(p);
f.setSize( 300 , 300 );
f.show();
}
public void actionPerformed(ActionEvent e)
{
try {
Robot r = new Robot();
int xi1, yi1, xi, yi;
Point p = MouseInfo.getPointerInfo().getLocation();
xi = p.x;
yi = p.y;
xi1 = Integer.parseInt(x.getText());
yi1 = Integer.parseInt(y.getText());
int i = xi, j = yi;
while (i != xi1 || j != yi1) {
r.mouseMove(i, j);
if (i < xi1)
i++;
if (j < yi1)
j++;
if (i > xi1)
i--;
if (j > yi1)
j--;
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 {
static JFrame f;
static TextField x, y, x1, y1;
robomouse1()
{
}
public static void main(String args[])
{
robomouse1 rm = new robomouse1();
f = new JFrame( "robomouse" );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = new TextField( 7 );
y = new TextField( 7 );
x1 = new TextField( 7 );
y1 = new TextField( 7 );
Button b = new Button( "OK" );
b.addActionListener(rm);
Panel p = new Panel();
Label l, l1;
l = new Label( "from" );
l1 = new Label( "to" );
p.add(l);
p.add(x);
p.add(y);
p.add(l1);
p.add(x1);
p.add(y1);
p.add(b);
f.add(p);
f.setSize( 600 , 300 );
f.show();
}
public void actionPerformed(ActionEvent e)
{
try {
Robot r = new Robot();
int xi, yi, xi1, yi1;
xi = Integer.parseInt(x.getText());
yi = Integer.parseInt(y.getText());
xi1 = Integer.parseInt(x1.getText());
yi1 = Integer.parseInt(y1.getText());
r.mouseMove(xi, yi);
r.mousePress(InputEvent.BUTTON1_MASK);
int i = xi, j = yi;
while (i < xi1 || j < yi1) {
r.mouseMove(i, j);
if (i < xi1)
i++;
if (j < yi1)
j++;
Thread.sleep( 30 );
}
Thread.sleep( 4000 );
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.
Last Updated :
07 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...