final MSG msg = new MSG();
@Override public void run() { MSG msg = new WinUser.MSG();
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 void run() { try { if (!isHooked) { hhk = USER32INST.SetWindowsHookEx(14, mouseHook, KERNEL32INST.GetModuleHandle(null), 0); isHooked = true; WinUser.MSG msg = new WinUser.MSG(); while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0) { USER32INST.TranslateMessage(msg); USER32INST.DispatchMessage(msg); //System.out.print(isHooked); if (!isHooked) { break; } } } else { } } catch (Exception e) { System.err.println(e.getMessage()); System.err.println("Caught exception in MouseHook!"); } } }, "Named thread");
@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")); }
@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 static void dispatchMessages() { // This bit never returns from GetMessage int result = -1; final MSG msg = new MSG(); while ( result != 0 ) { result = User32.INSTANCE.GetMessage( msg, null, 0, 0 ); if ( result == -1 ) { System.err.println( "error in get message" ); break; } else { User32.INSTANCE.TranslateMessage( msg ); try { User32.INSTANCE.DispatchMessage( msg ); } catch ( final Error e ) { e.printStackTrace(); } } } }
@Override public void run() { create(); synchronized (lock) { lock.notifyAll(); } MSG msg = new MSG(); while (User32.INSTANCE.GetMessage(msg, null, 0, 0) > 0) { User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); } destory(); }
public void startMainLoop() { mainLoopStarted = true; MSG msg = new MSG(); while (libU.GetMessage(msg, null, 0, 0) == 1) { libU.TranslateMessage(msg); libU.DispatchMessage(msg); } }
@Override public void run() { create(); synchronized (lock) { lock.notifyAll(); } MSG msg = new MSG(); while (User32.INSTANCE.GetMessage(msg, null, 0, 0) > 0) { User32.INSTANCE.DispatchMessage(msg); } destory(); }