获取在启动app / applescript时按下的修饰键

时间:2011-09-22 11:50:20

标签: macos applescript

我需要获取一个键列表(例如shift键,alt,命令...),这些键在我启动应用程序时被按下,尤其是Mac OS X上的ApplesScript。

3 个答案:

答案 0 :(得分:1)

请参阅StefanK在MacScripter / Tiger shiftKey detect error的第5篇文章。他编写了一个名为checkModifierKeys的命令行工具,这可能就是你所需要的。代码也已发布,因此您可以根据需要进行调整。

答案 1 :(得分:1)

Mike Woodfill回答的简短形式:

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'")

请注意,您将修饰符作为字符串获取,因此必须像这样if (altDown = "True") then ...

进行比较

如果您确实需要布尔修饰符,请查看以下代码:

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

答案 2 :(得分:0)

您可以使用以下代码来检测是否使用“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