Parcourir la source

need bugfix, when minimized, when fullscreen

ChesTeRcs il y a 5 ans
Parent
commit
2ae23ce391

+ 5 - 4
src/main/java/antiafk/AntiAfk.java

@@ -15,7 +15,7 @@ public class AntiAfk {
     // Config
     private String windowTitle = "Sea of Thieves"; // Target process title.
     private Integer afkTimerInMs = 5000; // antiafk.AntiAfk repeat cycle in ms.
-    private int antiAfkKey = KeyEvent.VK_ENTER; // Key to antiAfk.
+    private int antiAfkKey = KeyEvent.VK_ALT; // Key to antiAfk.
 
     // Global
     private Instant timeWhenAfkHappened = timeUntil.getNowInInstant().minusMillis(afkTimerInMs);
@@ -53,7 +53,6 @@ public class AntiAfk {
                     }
 
                     System.out.println("Last antiafk.AntiAfk: " + timeUntil.calcBetween(timeWhenAfkHappened));
-                    Thread.sleep(clock);
                 } else {
                     System.out.println("Not running: " + windowTitle);
                 }
@@ -78,17 +77,19 @@ public class AntiAfk {
     }
 
     public Pointer getActiveWindow() {
+        System.out.println(User32Util.getForegroundWindow());
         return User32Util.getForegroundWindow();
     }
 
     public boolean setActivateWindow(String windowTitle) {
         Pointer windowPointer = getWindowPointer(windowTitle);
-        User32Util.setForegroundWindow(windowPointer);
+        User32Util.setFocusToWindowsApp(windowTitle, 0);
         return winWaitActive(windowPointer);
     }
 
     public boolean setActivateWindow(Pointer windowPointer) {
-        return User32Util.setForegroundWindow(windowPointer);
+        User32Util.setFocusToWindowsApp(User32Util.getWindowText(windowPointer), 0);
+        return winWaitActive(windowPointer);
     }
 
     public void antiAfk(int antiAfkKeyParam) {

+ 1 - 1
src/main/java/antiafk/user32util/User32Ex.java

@@ -10,7 +10,7 @@ import com.sun.jna.ptr.PointerByReference;
 import com.sun.jna.win32.W32APIOptions;
 
 public interface User32Ex extends W32APIOptions {
-    User32Ex instance = (User32Ex) Native.loadLibrary("user32", User32Ex.class, DEFAULT_OPTIONS);
+    User32Ex INSTANCE = (User32Ex) Native.loadLibrary("user32", User32Ex.class, DEFAULT_OPTIONS);
 
     int SetWindowLongPtr(HWND hWnd, int nIndex, Callback callback);
 

+ 29 - 1
src/main/java/antiafk/user32util/User32Util.java

@@ -17,8 +17,36 @@ import java.util.List;
 
 public class User32Util {
     private static final User32Instance USER_32_INSTANCE = User32Instance.INSTANCE;
+    private static final User32Ex USER_32_EX = User32Ex.INSTANCE;
     private static Pointer callBackHwnd;
 
+
+    public static void setFocusToWindowsApp(String applicationTitle, int windowState) {
+        int state = windowState;
+        switch (state) {
+            default:
+            case 0:
+                state = User32.SW_SHOWNORMAL;
+                break;
+            case 1:
+                state = User32.SW_SHOWMAXIMIZED;
+                break;
+            case 2:
+                state = User32.SW_SHOWMINIMIZED;
+                break;
+        }
+
+        WinDef.HWND hWnd = USER_32_EX.FindWindow(null, applicationTitle);
+        if (USER_32_EX.IsWindowVisible(hWnd)) {
+            if (state != User32.SW_SHOWMINIMIZED) {
+                USER_32_EX.ShowWindow(hWnd, User32.SW_SHOWMINIMIZED);
+            }
+            USER_32_EX.ShowWindow(hWnd, state);
+            USER_32_EX.SetFocus(hWnd);
+        }
+    }
+
+
     public static boolean windowExists(final String startOfWindowName) {
         return !USER_32_INSTANCE.EnumWindows(new User32Instance.WNDENUMPROC() {
             @Override
@@ -157,7 +185,7 @@ public class User32Util {
         WinDef.POINT getPos = new WinDef.POINT();
         WinDef.RECT rect = new WinDef.RECT();
         User32Instance.INSTANCE.GetClientRect(hWnd, rect);
-        User32Ex.instance.ClientToScreen(hWnd, getPos);
+        User32Ex.INSTANCE.ClientToScreen(hWnd, getPos);
 
         return new Rectangle(getPos.x, getPos.y, rect.right, rect.bottom);
     }