代码之家  ›  专栏  ›  技术社区  ›  Kranthi Samala

如何使用java检测系统中的每个鼠标事件

  •  0
  • Kranthi Samala  · 技术社区  · 9 年前

    如何使用java检测系统中的每个鼠标事件?

    我尝试过用Point类捕捉鼠标的运动,但这并不方便。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Kranthi Samala    9 年前
    import org.jnativehook.GlobalScreen;
    import org.jnativehook.NativeHookException;
    import org.jnativehook.keyboard.NativeKeyEvent;
    import org.jnativehook.keyboard.NativeKeyListener;
    
    class GlobalKeyListenerExample implements NativeKeyListener {
        public void nativeKeyPressed(NativeKeyEvent e) {
                System.out.println("Key Pressed: " +         NativeKeyEvent.getKeyText(e.getKeyCode()));
    
                if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) {
                }
        }
    
        public void nativeKeyReleased(NativeKeyEvent e) {
                System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
        }
    
        public void nativeKeyTyped(NativeKeyEvent e) {
                System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
        }
        public GlobalKeyListenerExample()
        {
            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);
                }
            GlobalScreen.getInstance().addNativeKeyListener(this);
        }
        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 and initialze native hook.
                GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
        }
    

    }