我正在尝试将jnativehook
用于全局键盘侦听器,但似乎按键代码不同。例如,我要检查是否按下了ALT + I
:
addNativeKeyListener(new NativeKeyAdapter()
{
@Override
public void nativeKeyReleased(NativeKeyEvent keyEvent)
{
val modifiers = keyEvent.getModifiers();
val altPressed = modifiers == ALT_L_MASK;
LOGGER.log(INFO, "Modifiers: " + modifiers + " ALT: " + ALT_L_MASK);
if (!altPressed)
{
return;
}
val keyCode = keyEvent.getKeyCode();
LOGGER.log(INFO, "Key code: " + keyCode + " VK_I: " + VK_I);
if (keyCode != VK_I)
{
return;
}
LOGGER.log(INFO, "Button combination recognized...");
}
});
检查要按下的ALT
修饰符是否可以正常工作,但是检查I
按钮却不能正常工作:
INFO: Modifiers: 8 ALT: 8
Oct 06, 2018 2:38:44 PM com.myCompany.Main nativeKeyReleased
INFO: Key code: 23 VK_I: 73
为什么松开23
按钮时键代码I
是为什么?如何在不对那些看似随机的整数常量进行硬编码的情况下检查I
按钮? Java为密钥代码提供了KeyEvent
类,但是它们在这里不适用,对吗?