As you might have read from the title when I call Robot#mousePress my program halts, my code is as follows:
if(read == 2) {
System.out.println("Click down");
robot.mousePress(is.read());
}
else if(read == 3) {
System.out.println("Click up");
robot.mouseRelease(is.read());
}
Where:
Now whenever I send data from the client to the server (PacketID, MouseButton), the server reads both the PacketID and MouseButton correctly and without halting (tested with printing), however when I try to add Robot#mousePress/Robot#mouseRelease it stops working, the entire code is inside a Runnable schduled using a ScheduledExecutorService that runs every millisecond.
Example code:
import java.awt.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class Test {
public static void main(String[] args) throws AWTException {
Robot robot = new Robot();
AtomicBoolean bool = new AtomicBoolean();
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
System.out.println("hey");
if (!bool.get()) {
bool.set(true);
robot.mousePress(1);
}
}, 0, 1, TimeUnit.MILLISECONDS);
}
}
Instead of passing the direct number to the Robot method, put the number trough InputEvent.getMaskForButton(number)
Thanks to everyone that helped me :D
You are passing mousePress
an illegal argument, so it is throwing an exception. The exception won't get propagated out of the thread it's thrown in, so you will only see it if you call get()
on the ScheduledFuture
that scheduleAtFixedRate
returns.
The exception is:
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid combination of button flags
at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:195)
at Test.main(Test.java:26)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid combination of button flags
at Test.lambda$main$0(Test.java:22)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:654)
at java.base/java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:336)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:1521)
Caused by: java.lang.IllegalArgumentException: Invalid combination of button flags
at java.desktop/java.awt.Robot.checkButtonsArgument(Robot.java:316)
at java.desktop/java.awt.Robot.mousePress(Robot.java:252)
at Test.lambda$main$0(Test.java:18)