Why does my cursor teleport while looping Robot mousePress() and mouseRelease()?

51 Views Asked by At

Update: it seems like this issue only affects macOS users.

I'm making an auto clicker program. I'm using a while loop that spams left click whenever the auto clicker is enabled. However, after the auto clicker clicks the first time, every subsequent click snaps to the original click location (even though I never used the mouseMove() method). I've taken a look at other auto clicker implementations and can't see why mine would result in this issue.

I reproduced the issue with the following simple code:

import java.awt.*;
import java.awt.event.InputEvent;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Robot robot;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
        Thread.sleep(5000);
        for(int i = 0; i < 20; i++){
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            Thread.sleep(100);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            Thread.sleep(100);
        }
    }
}
0

There are 0 best solutions below