Home > Scripts > Java ‘Robot’ class for emulating mouse movement

Java ‘Robot’ class for emulating mouse movement


SITUATION: In your workstation/office-machine someone(probably your local admin) have implemented screen locking program which automatically locks your screen if you have been idle for a few min’s, and you don’t have ‘root’ level access to that machine; so you are not able to help yourself 😦 . The default lock time which was set on my local system(by my admin) was 1min. So, if I’ve been idle for 1min, then it will lock my screen using ‘xlock’ command and I’ve to retype my password to login again(which is pretty much annoying sometimes).



ASSUMPTIONS: JDK is installed and the java compiler(javac) and java runtime environment(java) are accessible to normal users.



SOLUTION: You can compile the following ‘java‘ program and run it to avoid yourself getting locked from your desktop continuously. This program uses java ‘Robot‘ class for emulating mouse movement. This program runs in an infinite ‘for‘ loop. It gets the current location of the mouse pointer(gets both ‘x’ and ‘y’ co-ordinates) using class “MouseInfo.getPointerInfo” and set the mouse position as the same, thus creating a movement which bypasses the screen lock.




import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;

public class RobotMouse {
    /* DELAY constant defines the time in milliseconds, which the for loop 
     should wait before initiating a mouse movement
    public static final int DELAY = 50000; 
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            // Creates the delay of 50 sec 
            // Robot start moving mouse 
            } catch (AWTException e) {



1) The default time delay set in the program is 50seconds(50000 ms). You can adjust the variable ‘DELAY‘ to set the time duration during which ‘Robot’ class initiates a mouse movement.
2) You have to save the java file in the same name as the main java class. Here it’s ‘RobotMouse.java‘.



First compile the program using ‘javac’ and then run it using ‘java’ command with ‘nohup’ prefix which runs the the program in background. So, even if you close the console it will still run in background.

reynold@jackal777~# javac RobotMouse.java 
reynold@jackal777~# ls
RobotMouse.class  RobotMouse.java
reynold@jackal777~# nohup java RobotMouse &
[1] 7143
reynold@jackal777~# nohup: ignoring input and appending output to `nohup.out'
reynold@jackal777~# jps
7232 Jps
7143 RobotMouse


Categories: Scripts
  1. shaik
    November 12, 2013 at 7:26 am

    how to run this program?

  2. January 9, 2014 at 3:25 am

    Hi Reynold,

    I have something to add. 🙂

    Was getting the error “java.awt.AWTException: headless environment” and app was using lot of CPU resources.
    2522 jithinp 20 0 673m 41m 7396 S 98 2.1 6:27.50 java RobotMouse

    We can update the app to disable checking this by adding “System.setProperty(“java.awt.headless”, “false”);” and that fixed my issues.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: