Get modifier keys, which have been pressed while starting an app / applescript

泄露秘密 提交于 2019-12-06 13:32:33

A shorter form of Mike Woodfill's answer:

set {shiftDown, ctrlDown, altDown, cmdDown} to words of (do shell script "python -c 'import Cocoa;m=Cocoa.NSEvent.modifierFlags();print m&Cocoa.NSShiftKeyMask>0,m&Cocoa.NSControlKeyMask>0,m&Cocoa.NSAlternateKeyMask>0,m&Cocoa.NSCommandKeyMask>0'")

Note, that you get the modifiers as strings, so you have to compare them like this if (altDown = "True") then ...

If you really want boolean modifiers, look at this code:

set mods to {}
repeat with m in words of (do shell script "python -c 'import Cocoa;m=Cocoa.NSEvent.modifierFlags();print m&Cocoa.NSShiftKeyMask,m&Cocoa.NSControlKeyMask,m&Cocoa.NSAlternateKeyMask,m&Cocoa.NSCommandKeyMask'")
    set end of mods to m as number as boolean
end repeat
set {shiftDown, ctrlDown, altDown, cmdDown} to mods
regulus6633

See post #5 by StefanK at MacScripter / Tiger shiftKey detect error. He wrote a command line tool called checkModifierKeys which may be what you need. The code is posted too so you can adjust it if needed.

You can use the following code to detect is modifier keys are being held down using "vanilla" applescript.

on isModifierKeyPressed()

set modiferKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false}

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask '") > 1 then
    set option_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask '") > 1 then
    set command_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSShiftKeyMask '") > 1 then
    set shift_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa. NSControlKeyMask '") > 1 then
    set control_down of modiferKeysDOWN to true
end if

return modiferKeysDOWN
end isModifierKeyPressed
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!