shau-kote
shau-kote

Reputation: 1150

Wait until any key is held in AppleScript?

I have an AppleScript background script which generates hotkey events by System Events. So I need stop the script before generate keystroke and wait until user releases all keys (otherwise effect will be unpredictable).

How can I get this done? I don't need exactly AppleScript solution. It does not matter, I can just call external Python/Bash/ObjC/Swift. Anything, but I only very dislike to run obscure and untrusted binaries.

Upvotes: 0

Views: 625

Answers (1)

graham bing
graham bing

Reputation: 69

I don't know about any key, but you can wait for modifier keys to be released.

eg. don't continue until user releases option and shift keys

use framework "AppKit"

property NSShiftKeyMask : a reference to 131072
property NSAlternateKeyMask : a reference to 524288
property NSEvent : a reference to current application's NSEvent

set modifier_down to true
repeat while modifier_down
    set modifier_flags to NSEvent's modifierFlags()
    set option_down to ((modifier_flags div (get NSAlternateKeyMask)) mod 2) = 1
    set shift_down to ((modifier_flags div (get NSShiftKeyMask)) mod 2) = 1
    set modifier_down to option_down or shift_down
end repeat

and here are keycodes for the other modifiers

property NSCapsLockMask : a reference to 65536
property NSControlKeyMask : a reference to 262144
property NSCommandKeyMask : a reference to 1048576
property NSFunctionKeyMask : a reference to 8388608

Upvotes: 1

Related Questions