简体   繁体   English

如何检查为 NativeKeyEvent 按下了哪些修饰符?

[英]How to check which modifiers were pressed for a NativeKeyEvent?

I am trying to write my first global hotkey listener using jnativehook.我正在尝试使用 jnativehook 编写我的第一个全局热键侦听器。 The modifiers part doesn't seem to have a clear description for beginners, but I'm sure this is a quick one to answer for someone who is familiar.修饰符部分似乎没有针对初学者的清晰描述,但我相信这对于熟悉的人来说是一个快速回答的问题。

I want to test if NativeInputEvent.META_MASK is in the modifiers field.我想测试NativeInputEvent.META_MASK是否在修饰符字段中。 Not knowing how to do this, I started searching through some discussions about the library.不知道该怎么做,我开始搜索有关图书馆的一些讨论。 Here , the author says it "works exactly the same as AWT modifiers in core Java" (most new developers are probably using swing or JavaFX so this does not make immediate sense to us). 在这里,作者说它“与核心 Java 中的 AWT 修饰符完全相同”(大多数新开发人员可能使用 swing 或 JavaFX,所以这对我们来说没有直接意义)。 I searched for an explanation for AWT modifiers and found this which says "You can check whether any modifier key was pressed by ANDing its constant with the modifiers field".我搜索了有关 AWT 修饰符的解释,发现说“您可以通过将其常量与修饰符字段进行 AND 操作来检查是否按下了任何修饰键”。

So, I don't know exactly how bitwise and or bitwise or work.所以,我不确切知道按位和/或按位或如何工作。 I looked it up, and from here it says bitwise and takes two bits, and if both bits are 1 it returns 1. I don't understand how this applies to comparing the two integers: NativeInputEvent.META_MASK and the modifiers field of the event.我查了一下,从这里它说按位并占用两位,如果两位都是 1,则返回 1。我不明白这如何适用于比较两个整数: NativeInputEvent.META_MASK和事件的修饰符字段.

What am I missing?我错过了什么?

You need to use the modifier bit-masks:您需要使用修饰符位掩码:

void nativeKeyTyped(NativeKeyEvent nativeEvent) {
    if (nativeEvent.getModifiers() & NativeInputEvent.META_MASK) {
        // META_MASK (left or right) was pressed.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM