From 75044e6e68a1124db4cec7e805fa46441ec2503c Mon Sep 17 00:00:00 2001 From: boholder Date: Sun, 2 Jul 2023 21:14:22 +0800 Subject: [PATCH] Fix the failure that mouse click event is performed before mouse cursor moving event in Windows, close #65 --- .../minecraft_access/utils/MouseUtils.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/src/main/java/com/github/khanshoaib3/minecraft_access/utils/MouseUtils.java b/common/src/main/java/com/github/khanshoaib3/minecraft_access/utils/MouseUtils.java index 4b1764cc..cf4cb715 100644 --- a/common/src/main/java/com/github/khanshoaib3/minecraft_access/utils/MouseUtils.java +++ b/common/src/main/java/com/github/khanshoaib3/minecraft_access/utils/MouseUtils.java @@ -7,6 +7,7 @@ import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.TimeUnit; /** * Contains functions to simulate mouse events. @@ -21,6 +22,15 @@ public class MouseUtils { */ public static void moveAndLeftClick(int x, int y) { move(x, y); + // fix the https://github.com/khanshoaib3/minecraft-access/issues/65 + if (OsUtils.isWindows()) { + try { + // with a little bit of waiting, everything is ok now. + // I've tried to set the value to 10, and it doesn't always work, 20 is fine. + TimeUnit.MILLISECONDS.sleep(20); + } catch (Exception ignored) { + } + } leftClick(); } @@ -31,6 +41,12 @@ public static void moveAndLeftClick(int x, int y) { */ public static void moveAndRightClick(int x, int y) { move(x, y); + if (OsUtils.isWindows()) { + try { + TimeUnit.MILLISECONDS.sleep(20); + } catch (Exception ignored) { + } + } rightClick(); }