如何使用给定的系统挂钩检测Windows按键

时间:2017-12-11 16:21:48

标签: java windows keylistener

我正在使用链接中的代码:system-hook

import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;

public class Demo {
    private static GlobalKeyboardHook hook;
    private static boolean run = true;

    public static void main(String[] args) {
        hook = new GlobalKeyboardHook(true);
        hook.addKeyListener(new GlobalKeyAdapter() {
            @Override
            public void keyPressed(GlobalKeyEvent event) {
                if (event.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE)
                    run = false;
            }

            @Override
            public void keyReleased(GlobalKeyEvent event) {
                String modifier = "";
                if (event.isShiftPressed()) modifier += "SHIFT + ";
                if (event.isMenuPressed()) modifier += "ALT + ";
                if (event.isControlPressed()) modifier += "CTRL + ";
                if (isWindowsPressed(event)) modifier += "WINDOWS + ";
                if (modifier != "") {
                    System.out.println(modifier + event.getKeyChar());
                }

            }
        });


        try {
            while (run) Thread.sleep(100);
        } catch (InterruptedException e) { /* nothing to do here */ } finally {
            System.out.println("Shutting down");
            hook.shutdownHook();
        }

    }

    private static boolean isWindowsPressed(GlobalKeyEvent event) {
        return event.getVirtualKeyCode() == GlobalKeyEvent.VK_RWIN || event.getVirtualKeyCode() == GlobalKeyEvent.VK_LWIN;//LWIN,RWIN is left and right windows key
    }
}

此代码适用于Ctrl + N,Ctrl + S等组合 我无法弄清楚如何检测Window键按下以检测Win + e或Win + x组合。 isWindowsPressed()似乎是个问题。

0 个答案:

没有答案