public static void main(String[] args) throws Exception { HOOKPROC hookProc = new HOOKPROC_bg(); HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null); User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0); if (hHook == null) return; User32.MSG msg = new User32.MSG(); System.err.println("Please press any key ...."); while (true) { User32.INSTANCE.GetMessage(msg, null, 0, 0); } }
public synchronized void registerMouseUpListner(Runnable action, Executor executor) { if (thrd != null) return; this.action = action; thrd = new Thread(new Runnable() { public void run() { try { if (!isHooked) { hhk = USER32INST.SetWindowsHookEx(14, (HOOKPROC) mouseHook, KERNEL32INST.GetModuleHandle(null), 0); isHooked = true; MSG msg = new MSG(); while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0) { System.out.println("got message"); USER32INST.TranslateMessage(msg); USER32INST.DispatchMessage(msg); System.out.print(isHooked); if (!isHooked) break; } } else System.out.println("The Hook is already installed."); } catch (Exception e) { System.err.println(e.getMessage()); System.err.println("Caught exception in MouseHook!"); } // System.out.println("terminated "); USER32INST.UnhookWindowsHookEx(hhk); hhk = null; } }, "Named thread"); threadFinish = false; thrd.start(); }