1

I am trying to write my first global hotkey listener using 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. 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). 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".

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.

What am I missing?

Sujal Kumar
  • 1,054
  • 10
  • 23
Matt Groth
  • 470
  • 4
  • 20

1 Answers1

0

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.
    }
}
Alex Barker
  • 4,316
  • 4
  • 28
  • 47