在 applescript 中检测用户的按键是不可能的。不过,您可以通过编程方式按键。要解决按住键的问题,请使用“key down”命令并在需要释放它时发出“key up”命令。这将适用于任何应用程序。这是一个例子。
tell application "KeyboardViewer" to activate
tell application "System Events"
try --don't even consider not using a try block because down keys can get stuck!
key down control
delay 1
key down shift
delay 1
key down option
delay 1
key down command
delay 1
key up control
delay 1
key up shift
delay 1
key up option
delay 1
key up command
delay 1
key down {control, shift, option, command}
delay 1
key up {control, shift, option, command}
on error --logging out is the only other way to unstick these
key up {control, shift, option, command}
end try
end tell
tell application "KeyboardViewer" to quit
注意:如果您想按顺序按下和释放某些键,也可以使用“击键”命令。例如,要按 command-s,您可以执行以下操作:
tell application "System Events"
keystroke "s" using command down
end tell