So my problem is that i want to simulate mouse clicks while the mouse is held down. But the Robot library cant execute the click while the physical button is held down.
This is my code for the click:
public static void click() {
Runnable runnable = () -> {
try {
Robot robot = new Robot();
while (clicking) {
System.out.println("Clicking! " + System.currentTimeMillis());
robot.mousePress(mouseButton ? InputEvent.BUTTON1_DOWN_MASK : InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(300);
robot.mouseRelease(mouseButton ? InputEvent.BUTTON1_DOWN_MASK : InputEvent.BUTTON2_DOWN_MASK);
}
} catch (Exception ignored) {
System.out.println("Couldn't click");
}
};
new Thread(runnable).start();
}
This is my mouse event listener code:
public class MouseListener implements NativeMouseInputListener {
public void nativeMousePressed(NativeMouseEvent e) {
if (active && !focused && !clicking && e.getButton() == (mouseButton ? 1 : 2)) {
clicking = true;
click();
}
}
public void nativeMouseReleased(NativeMouseEvent e) {
if (active && !focused && e.getButton() == (mouseButton ? 1 : 2)) {
clicking = false;
}
}
}
The print gets executed normally and if i switch the clicks so a left click gets executed when i do a rightclick and that also works but i want it with the same button. I appreciate your help.