Top community members

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

Java - simple auto mouse mover application

3 points
Asked by:
Ticket Ninja
2332

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:
Ticket Ninja
2332

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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more