Detect key pressed

Is it possible to detect which key is pressed when I execute a script?
I want to assign 2 key shortcuts to the same script (one with shift) and evaluate if this key is pressed to display a window or not.

I don’t think it is possible, but I have a look.

Thanks Georg.

You could make two scripts, give each of them one shortcut and then call your script accordingly.

To freshen this up, you can query the currently pressed modifier keys:

from AppKit import NSEvent # you don't need this if run within Glyphs
optionKeyFlag = 524288
optionKeyPressed = NSEvent.modifierFlags() & optionKeyFlag == optionKeyFlag
if optionKeyPressed:
	print "Option is pressed!"
else:
	print "No Option key pressed."

Try for yourself, print NSEvent.modifierFlags() with different keys held down to see what the key flags are. Or import the (officially deprecated) constants: Running scripts with different key combos for different behaviour

1 Like