Detect if ctrl key is pressed
-
29-04-2021 - |
Pregunta
In JavaScript for Automation (JXA), we can detect if the ctrl key is pressed with
ObjC.import("Cocoa")
$.NSEvent.modifierFlags & $.NSControlKeyMask
If ctrl is not pressed, the return will be 0
. If it is, the value will be different.
But NSControlKeyMask
is deprecated. I found NSEventModifierFlagControl
but can’t get it to work.
What is the current way to detect if the ctrl key is pressed?
Solución
Per your latest comment, you state that the code you have quoted works in practice, and your only problem is you want to get rid of the deprecated NSControlKeyMask.
Simply replace that word with NSEventModifierFlagControl and this is it.
The only thing Apple did is to change the name. In recent macOS NSControlKeyMask is even defined as simply being equal to NSEventModifierFlagControl. Both have the value 1 << 18 (i.e. the 18th bit is set to 1, rest is 0).