@Override public synchronized void hook() { MouseHook mouseHook = new MouseHook(createMouseEventLogger()); AtomicReference<User32.HHOOK> hhookRef = new AtomicReference<>(); Thread hookMouseEventsThread = new Thread(() -> { this.mouseHookThreadID = Kernel32.INSTANCE.GetCurrentThreadId(); HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null); User32.HHOOK hhook = User32.INSTANCE.SetWindowsHookEx(User32.WH_MOUSE_LL, mouseHook, hInst, 0); boolean mouseEventsHooked = hhook != null; if (!mouseEventsHooked) { LOG.error("Failed to hook mouse events"); return; } hhookRef.set(hhook); LOG.info("Mouse events hooked"); User32.INSTANCE.GetMessage(new WinUser.MSG(), new WinDef.HWND(Pointer.NULL), 0, 0); User32.INSTANCE.UnhookWindowsHookEx(hhook); this.mouseHookThreadID = 0; LOG.info("Mouse events unhooked"); }); hookMouseEventsThread.setName("MouseHookThread"); hookMouseEventsThread.start(); awaitUtils.waitFor(() -> hhookRef.get() != null, 2000L, () -> LOG.error("Failed to hook mouse events in time")); }
@Override public synchronized void hook() { KeyboardHook keyboardHook = new KeyboardHook(createKeyboardEventLogger()); AtomicReference<User32.HHOOK> hhookRef = new AtomicReference<>(); Thread hookKeyboardEventsThread = new Thread(() -> { this.keyboardHookThreadID = Kernel32.INSTANCE.GetCurrentThreadId(); WinDef.HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null); User32.HHOOK hhook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, keyboardHook, hInst, 0); boolean keyboardEventsHooked = hhook != null; if (!keyboardEventsHooked) { LOG.error("Failed to hook keyboard events"); return; } hhookRef.set(hhook); LOG.info("Keyboard events hooked"); User32.INSTANCE.GetMessage(new WinUser.MSG(), new WinDef.HWND(Pointer.NULL), 0, 0); User32.INSTANCE.UnhookWindowsHookEx(hhook); this.keyboardHookThreadID = 0; LOG.info("Keyboard events unhooked"); }); hookKeyboardEventsThread.setName("KeyboardHookThread"); hookKeyboardEventsThread.start(); awaitUtils.waitFor(() -> hhookRef.get() != null, 2000L, () -> LOG.error("Failed to hook keyboard events in time")); }
/** * Get the native window handle. * * @return The handle * @throws AutomationException Something is wrong in automation */ public WinDef.HWND getNativeWindowHandle() throws AutomationException { Object value = this.element.getPropertyValue( PropertyID.NativeWindowHandle.getValue()); return new WinDef.HWND(Pointer.createConstant( Integer.valueOf(value.toString()))); }
public WindowsPowerXP() { if (!Kernel32Ex.INSTANCE.SetProcessShutdownParameters(0x03FF, 0)) throw new GetLastErrorException(); mp.start(); if (!Kernel32Ex.INSTANCE.SetConsoleCtrlHandler(hr, true)) throw new GetLastErrorException(); final HWND hwnd = new HWND(); f.pack(); hwnd.setPointer(Native.getComponentPointer(f)); int wID = User32.INSTANCE.GetWindowThreadProcessId(hwnd, null); hHook = User32.INSTANCE.SetWindowsHookEx(WH_CALLWNDPROC, hp, null, wID); if (hHook == null) throw new GetLastErrorException(); }
/** * Get a window handle for a component identifier. * * @param componentId component identifier * @return native window handle */ private HWND getHWND(long componentId) { return new HWND(Pointer.createConstant(componentId)); }
private static WinDef.HWND getHWnd(Component w) { WinDef.HWND hwnd = new WinDef.HWND(); hwnd.setPointer(Native.getComponentPointer(w)); return hwnd; }
private HWND getHWnd(Component w) { HWND hwnd = new HWND(); hwnd.setPointer(Native.getComponentPointer(w)); return hwnd; }
private HWND getHWnd(Component w) { HWND hwnd = new HWND(); hwnd.setPointer(Native.getComponentPointer(w)); return hwnd; }