Working example of JNA mouse hook

前端 未结 2 487
滥情空心
滥情空心 2020-11-30 11:58

Can any one provide me with a working example of JNA mouse hook, which would be able to track mouse movements/click outside my Java Swing application ?

Thanks in Adv

2条回答
  •  青春惊慌失措
    2020-11-30 12:37

    movements:

    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());
        }
    }
    

    click:

    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);
            }
    }
    

提交回复
热议问题