@Override public synchronized void unhook() { LOG.info("Interrupting mouse events hook thread"); User32.INSTANCE.PostThreadMessage(mouseHookThreadID, WinUser.WM_QUIT, new WinDef.WPARAM(), new WinDef.LPARAM()); awaitUtils.waitFor(() -> mouseHookThreadID == 0, 2000L, () -> LOG.error("Failed to unhook mouse events in time")); }
@Override public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, WinUser.KBDLLHOOKSTRUCT lParam) { int code = wParam.intValue(); boolean keyPressed = isKeyPressed(code); if (keyPressed) { keyboardEventListener.keyPressed(lParam.vkCode); } boolean keyReleased = isKeyReleased(code); if (keyReleased) { keyboardEventListener.keyReleased(lParam.vkCode); } if (!keyPressed && !keyReleased) { LOG.error("Unknown key event: {}", code); } return new WinDef.LRESULT(0); }
.SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_BIG), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber); .SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_SMALL), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber); .SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_SMALL2), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber);
@Override public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, MOUSEHOOKSTRUCT lParam) { int code = wParam.intValue(); if (code == WM_MOUSEMOVE) { mouseEventListener.mouseMove(new Point(lParam.pt.x, lParam.pt.y));
int nID = wParam.intValue() & 0xff; MenuMap m = hMenusIDs.get(nID); m.fire();
if (hWnd == null) break; User32Ex.INSTANCE.SendMessage(hWnd, WM_KEYDOWN, new WPARAM(VK_ESCAPE), null);
@Override public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, MSLLHOOKSTRUCT info) { if (nCode >= 0) { switch (wParam.intValue()) { case JNIMouseHook.WM_LBUTTONDOWN: addEventToQueue(info.pt.x, info.pt.y); break; case JNIMouseHook.WM_RBUTTONDOWN: addEventToQueue(info.pt.x, info.pt.y); break; case JNIMouseHook.WM_MBUTTONDOWN: addEventToQueue(info.pt.x, info.pt.y); break; case JNIMouseHook.WM_MOUSEMOVE: break; default: break; } // unhook if needed if (threadFinish == true) { USER32INST.PostQuitMessage(0); } } Pointer ptr = info.getPointer(); long peer = Pointer.nativeValue(ptr); return USER32INST.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer)); } };
/** * Get the current state of a window. * * @param hWnd native window handle * @return window state */ private WindowState getWindowState(HWND hWnd) { WindowState windowState = new WindowState(); windowState.setMaximized(ExtendedUser32.INSTANCE.IsZoomed(hWnd)); if (windowState.getMaximized()) { ExtendedUser32.INSTANCE.SendMessage(hWnd, User32.WM_SYSCOMMAND, new WPARAM(ExtendedUser32.SC_RESTORE), new LPARAM(0)); } windowState.setStyle(ExtendedUser32.INSTANCE.GetWindowLong(hWnd, ExtendedUser32.GWL_STYLE)); windowState.setExStyle(ExtendedUser32.INSTANCE.GetWindowLong(hWnd, ExtendedUser32.GWL_EXSTYLE)); RECT rect = new RECT(); boolean gotWindowRect = ExtendedUser32.INSTANCE.GetWindowRect(hWnd, rect); if (gotWindowRect) { windowState.setLeft(rect.left); windowState.setTop(rect.top); windowState.setRight(rect.right); windowState.setBottom(rect.bottom); } return windowState; }
/** * Set the full-screen state of the window. * * @param fullScreen <code>true</code> to set full-screen; <code>false</code> to exit full-screen */ void setFullScreen(boolean fullScreen) { HWND hWnd = getHWND(Native.getComponentID(window)); if (fullScreen) { windowState = getWindowState(hWnd); ExtendedUser32.INSTANCE.SetWindowLong(hWnd, GWL_STYLE, windowState.getStyle() & ~(WS_CAPTION | WS_THICKFRAME)); ExtendedUser32.INSTANCE.SetWindowLong(hWnd, GWL_EXSTYLE, windowState.getExStyle() & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); MONITORINFO monitorInfo = getMonitorInfo(hWnd); RECT rect = monitorInfo.rcMonitor; ExtendedUser32.INSTANCE.SetWindowPos(hWnd, null, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); } else { ExtendedUser32.INSTANCE.SetWindowLong(hWnd, GWL_STYLE, windowState.getStyle()); ExtendedUser32.INSTANCE.SetWindowLong(hWnd, GWL_EXSTYLE, windowState.getExStyle()); ExtendedUser32.INSTANCE.SetWindowPos(hWnd, null, windowState.getLeft(), windowState.getTop(), windowState.getRight() - windowState.getLeft(), windowState.getBottom() - windowState.getTop(), SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); if (windowState.getMaximized()) { ExtendedUser32.INSTANCE.SendMessage(hWnd, User32.WM_SYSCOMMAND, new WPARAM(WinUser.SC_MAXIMIZE), new LPARAM(0)); } } }
/** * On session change. * * @param wParam the w param * @param lParam the l param */ protected void onSessionChange(WinDef.WPARAM wParam, WinDef.LPARAM lParam) { switch (wParam.intValue()) { case Wtsapi32.WTS_SESSION_LOCK: { this.onMachineLocked(lParam.intValue()); break; } case Wtsapi32.WTS_SESSION_UNLOCK: { this.onMachineUnlocked(lParam.intValue()); break; } } }
int WM_CLOSE = 0x10; HWND hwnd = User32.INSTANCE.FindWindow(null, windowTitle); User32.INSTANCE.PostMessage(hwnd, WM_CLOSE, new WinDef.WPARAM(), new WinDef.LPARAM());
@Override public void remove() { hide(); super.remove(); User32.User32.PostMessage(WindowsEventDispatch.get(), WM_QUIT, new WPARAM(0), new LPARAM(0)); } };
public void setDisplaySleep(final boolean sleep) { user32.DefWindowProc( getForegroundWindow().get(), WinUser.WM_SYSCOMMAND, new WinDef.WPARAM(SC_MONITORPOWER), new WinDef.LPARAM(sleep ? 2 : -1) ); }
@Override public synchronized void unhook() { LOG.info("Interrupting keyboard events hook thread"); User32.INSTANCE.PostThreadMessage(keyboardHookThreadID, WinUser.WM_QUIT, new WinDef.WPARAM(), new WinDef.LPARAM()); awaitUtils.waitFor(() -> keyboardHookThreadID == 0, 2000L, () -> LOG.error("Failed to unhook keyboard events in time")); }