检测按键并延迟直到按键释放AppleScript

检测按键并延迟直到按键释放AppleScript,applescript,keypress,Applescript,Keypress,我不知道如何检测AppleScript中按下的键,以及如何延迟直到释放该键。我想做一个缩放切换,我还有其他的东西(我想)。这是我当前的代码 on idle set ztoggle to 0 repeat --how do i make it so a key is needed to run this loop? maybe an 'if (im not sure what to put here) then' loop?-- if (

我不知道如何检测AppleScript中按下的键,以及如何延迟直到释放该键。我想做一个缩放切换,我还有其他的东西(我想)。这是我当前的代码

on idle
    set ztoggle to 0

    repeat

        --how do i make it so a key is needed to run this loop? maybe an 'if (im not sure what to put here) then' loop?--
            if (ztoggle = 1) then
                set ztoggle to 0
            else if (ztoggle = 0) then
                set ztoggle to 1
            end if
        --how do i make it so the program waits at this line until the key from before is released? i was thinking delay, but im not sure--


        if (ztoggle = 1) then
            tell application "System Events"
                key code 28 using {option down, command down}
            end tell
        end if

        set ztoggle to 0

    end repeat
end idle
有人知道我会怎么做吗?另外,这是我第一次用AppleScript尝试任何东西,所以如果我在其他地方搞砸了,请告诉我。

这在(“香草”)AppleScript中是不可能的。唯一接近它的是使用第三方命令行二进制文件“checkModifierKeys”([edit:new url-谢谢,@jerry-t])检查是否按下了修改器键

你得用像这样的东西

do shell script "/usr/local/bin/checkModifierKeys control"

在重复循环中。它确实工作得很好。

这可以在不添加任何第三方的情况下完成,但需要调用cocoa框架:

使用框架“Cocoa”
使用脚本添加
全球ca
将ca设置为当前应用程序
到isModifierPressed(修改器)
((ca的NSEvent的modifierFlags())/modifier为整数)mod 2等于1
结束IsModifier
重复此操作,直到按下IsModifier(ca的N SentModifierFlagControl)
延迟0.1—sad轮询/等待循环:(
结束重复
显示对话框“控件已按下”
请注意,
NSEvent的modifierFlags()
是一个位字段,
NSEventModifierFlagControl
是一个2n位的标志,需要将它们按位and合并,以查看是否保留了该键。AppleScript没有按位运算符(!!),因此在这种情况下,and是通过算术模拟的-首先通过整数除以modifierFlags()来模拟的NSEventModifierFlagControl位标志的位字段,然后查看结果是否为奇数

modifierFlags()
只报告即时修改器状态,因此我们必须在循环中轮询它以等待按键

也可以通过这种方式进行检查。它们是:

语法 描述 NSEventModifierFlagCapsLock 已按下Caps Lock键。 NSEventModifierFlagShift 已按下Shift键。 NSEventModifierFlagControl 已按下控制键。 NSEventModifierFlagOption 已按下选项或Alt键。 nseventmodifierflag命令 已按下命令键。 NSEventModifierFlagNumericPad 已按下数字键盘中的键或箭头键。 NSEventModifierFlagHelp 已按下帮助键。 nsEventModifierFlag函数 已按下功能键。 NSeventModifierFlagsDeviceIndependentFlagsMask 与设备无关的修改器标志被屏蔽。
这个链接已经死了。我在github上找到了这个链接(不确定是同一个作者):效果很好。