@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")); }
import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef; . . . //execute process Process p = Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\""); //wait for return value int res = p.waitFor(); //if we have a valid return code begin waiting for window to be closed if(res == 0) { //define a window handle variable WinDef.HWND windowHandle = null; do { //sleep a little while before polling the value try{Thread.sleep(100);}catch(InterruptedException e){} //try to fetch the window by title windowHandle = User32.INSTANCE.FindWindow(null, "<Window Title>"); //if the handle is not null, the window is still open so sleep and then try try again }while(windowHandle != null && windowHandle.getPointer() != Pointer.NULL); //continue on with your code }
mouseEventListener.mouseMove(new Point(lParam.pt.x, lParam.pt.y)); } else if (code == WM_MOUSEWHEEL) { long hwndValue = Pointer.nativeValue(lParam.hwnd.getPointer()); long hwndValue = Pointer.nativeValue(lParam.hwnd.getPointer());
public WinLockListener() { // define new window class final WString windowClass = new WString("MyWindowClass"); final WinDef.HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle(""); WinUser.WNDCLASSEX wClass = new WinUser.WNDCLASSEX(); wClass.hInstance = hInst; wClass.lpfnWndProc = WinLockListener.this; wClass.lpszClassName = windowClass.toString(); // register window class User32.INSTANCE.RegisterClassEx(wClass); getLastError(); // create new window final WinDef.HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass.toString(), "'hidden helper window to catch Windows events", 0, 0, 0, 0, 0, null, // WM_DEVICECHANGE contradicts parent=WinUser.HWND_MESSAGE null, hInst, null); getLastError(); Log.debug("window sucessfully created! window hwnd: " + hWnd.getPointer().toString()); Wtsapi32.INSTANCE.WTSRegisterSessionNotification(hWnd, Wtsapi32.NOTIFY_FOR_THIS_SESSION); WinUser.MSG msg = new WinUser.MSG(); while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) != 0) { User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); } /// This code is to clean at the end. You can attach it to your custom application shutdown listener Wtsapi32.INSTANCE.WTSUnRegisterSessionNotification(hWnd); User32.INSTANCE.UnregisterClass(windowClass.toString(), hInst); User32.INSTANCE.DestroyWindow(hWnd); Log.debug("program exit!"); }
@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")); }
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 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()))); }
/** * 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; }