Languages

Java - simple auto mouse mover application

3 points
Asked by:
Marcin
2544

How to write simple automatic mouse movement app in java?

Is it possible to write very simple app to do only that, without any additional logic.

I am only interested in couple of mouse moves from point 1 to point 2 on my computer screen. On the web I can find kind of complicated apps that do a lot of different stuff.

1 answers
3 points
Answered by:
Marcin
2544

Quick solution:

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

public class SimpleAutoMouseMover extends JPanel {

    public static void main(String[] args) throws Exception {

        JFrame frame = new JFrame();
        frame.setSize(450, 350);
        frame.setLocation(300, 300);
        frame.setVisible(true);

        Robot robot = new Robot();

        for (int i = 0; i < 3; i++) {
            System.out.println("move to 300px, 300px");
            robot.mouseMove(300, 300);

            System.out.println("sleep 3 sec");
            Thread.sleep(3000);

            System.out.println("move to 200px, 200px");
            robot.mouseMove(200, 200);

            System.out.println("sleep 3 sec");
            Thread.sleep(3000);
        }

        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
        frame.dispose();

        System.exit(0);
    }
}

Output:

move to 300px, 300px
sleep 3 sec
move to 200px, 200px
sleep 3 sec
move to 300px, 300px
sleep 3 sec
move to 200px, 200px
sleep 3 sec
move to 300px, 300px
sleep 3 sec
move to 200px, 200px
sleep 3 sec

This simple app will do exactly what you ask in question, move the mouse from one point to another. In this case from point(300px, 300px) to point(200px, 200px) and back again - it will be repeated 3 times. 

Flow:

  • from point(300px, 300px) to point(200px, 200px)
  • sleep 3 seconds
  • from point(200px, 200px) to point(300px, 300px)
  • sleep 3 seconds
  • repeat again

We can use while (true) instead of for loop, but before using while true - think twice and add some break condition.

This simple app can be easily modified and we can add some more features like auto click. Java has nice API to do those things.

0 comments Add comment
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join