JNA鼠标钩子的工作示例

时间:2010-08-28 09:32:23

标签: jna

任何人都可以为我提供JNA鼠标钩子的工作示例,它能够跟踪Java Swing应用程序外部的鼠标移动/点击吗?

先谢谢

2 个答案:

答案 0 :(得分:5)

是的,这是代码......

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:
                        // do stuff
                        break;
                    case CWMouseHook.WM_RBUTTONDOWN:
                        //do stuff
                        break;
                    case CWMouseHook.WM_MBUTTONDOWN:
                        //do other stuff
                        break;
                    case CWMouseHook.WM_LBUTTONUP:
                         //do even more stuff
                         break;
                    case CWMouseHook.WM_MOUSEMOVE:

                        break;                         
                    default:
                        break;
                }
                 /****************************DO NOT CHANGE, this code unhooks mouse *********************************/
                 if (threadFinish == true)
                  {                      
                     USER32INST.PostQuitMessage(0);
                  }
                /***************************END OF UNCHANGABLE *******************************************************/
            }
            return USER32INST.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
        }
    };
}
public class Point extends Structure
{
    public class ByReference extends Point implements Structure.ByReference {};
    public NativeLong x;
    public NativeLong y;
}
public static class MOUSEHOOKSTRUCT extends Structure
{
    public static class ByReference extends MOUSEHOOKSTRUCT implements Structure.ByReference {};
    public POINT pt;
    public HWND hwnd;
    public int wHitTestCode;
    public ULONG_PTR dwExtraInfo;
}

这就是它的全部内容。干杯。 这基本上是Sun论坛中一个人的代码的翻版...但也经过了我的测试,它再次为此欢呼。

编辑:我编辑了代码,因此它包含了LowLevelMouseProc,但您可以使用您可以在别处定义的HOOK扩展名。没关系那么重要。要小心,因为某些原因你必须将变量mouseHook作为静态,否则钩子只是在一段时间后解开。

答案 1 :(得分:0)

动作:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.mouse.NativeMouseWheelEvent;
import org.jnativehook.mouse.NativeMouseWheelListener;

public class GlobalMouseWheelListenerExample implements NativeMouseWheelListener {
    public void nativeMouseWheelMoved(NativeMouseWheelEvent e) {
        System.out.println("Mosue Wheel Moved: " + e.getWheelRotation());
    }

    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();

            System.exit(1);
        }

        // Construct the example object and initialze native hook.
        GlobalScreen.getInstance().addNativeMouseWheelListener(new GlobalMouseWheelListenerExample());
    }
}

点击:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;

public class GlobalMouseListenerExample implements NativeMouseInputListener {
        public void nativeMouseClicked(NativeMouseEvent e) {
                System.out.println("Mosue Clicked: " + e.getClickCount());
        }

        public void nativeMousePressed(NativeMouseEvent e) {
                System.out.println("Mosue Pressed: " + e.getButton());
        }

        public void nativeMouseReleased(NativeMouseEvent e) {
                System.out.println("Mosue Released: " + e.getButton());
        }

        public void nativeMouseMoved(NativeMouseEvent e) {
                System.out.println("Mosue Moved: " + e.getX() + ", " + e.getY());
        }

        public void nativeMouseDragged(NativeMouseEvent e) {
                System.out.println("Mosue Dragged: " + e.getX() + ", " + e.getY());
        }

        public static void main(String[] args) {
                try {
                        GlobalScreen.registerNativeHook();
                }
                catch (NativeHookException ex) {
                        System.err.println("There was a problem registering the native hook.");
                        System.err.println(ex.getMessage());

                        System.exit(1);
                }

                //Construct the example object.
                GlobalMouseListenerExample example = new GlobalMouseListenerExample();

                //Add the appropriate listeners for the example object.
                GlobalScreen.getInstance().addNativeMouseListener(example);
                GlobalScreen.getInstance().addNativeMouseMotionListener(example);
        }
}