Yes, here is the code ...
public class CWMouseHook { public final User32 USER32INST; public final Kernel32 KERNEL32INST; public CWMouseHook() { if(!Platform.isWindows()) { throw new UnsupportedOperationException("Not supported on this platform."); } USER32INST = User32.INSTANCE; KERNEL32INST = Kernel32.INSTANCE; mouseHook=hookTheMouse(); Native.setProtected(true); } public static LowLevelMouseProc mouseHook; public HHOOK hhk; public Thread thrd; public boolean threadFinish = true; public boolean isHooked = false; public static final int WM_MOUSEMOVE = 512; public static final int WM_LBUTTONDOWN = 513; public static final int WM_LBUTTONUP = 514; public static final int WM_RBUTTONDOWN = 516; public static final int WM_RBUTTONUP = 517; public static final int WM_MBUTTONDOWN = 519; public static final int WM_MBUTTONUP = 520; public void unsetMouseHook() { threadFinish = true; if (thrd.isAlive()) { thrd.interrupt(); thrd = null; } isHooked = false; } public boolean isIsHooked() { return isHooked; } public void setMouseHook() { thrd = new Thread(new Runnable() { @Override public void run() { try { if(!isHooked) { hhk = USER32INST.SetWindowsHookEx(14, mouseHook,KERNEL32INST.GetModuleHandle(null),0); isHooked = true; MSG msg = new MSG(); while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0) { 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!"); } } },"Named thread"); threadFinish = false; thrd.start(); } private interface LowLevelMouseProc extends HOOKPROC { LRESULT callback(int nCode, WPARAM wParam, MOUSEHOOKSTRUCT lParam); } public LowLevelMouseProc hookTheMouse() { return new LowLevelMouseProc() { @Override public LRESULT callback(int nCode, WPARAM wParam, MOUSEHOOKSTRUCT info) { if (nCode >= 0) { switch(wParam.intValue()) { case CWMouseHook.WM_LBUTTONDOWN:
It is all about him. Greetings. This is basically a breakdown of the code from the guy on the Sun forums ... but also tested by me, and it works so happy again.
Edit: I edited the code, so it includes LowLevelMouseProc, but you can use the HOOK extension, which you can define elsewhere. It does not really matter. Be careful that for some reason you have the mouseHook variable as static, otherwise the hook just gets unhooked after a while.
Nikola Yovchev
source share