Autohotkey 使用GetKeyState()和循环

Autohotkey 使用GetKeyState()和循环,autohotkey,Autohotkey,我正在尝试编写一个脚本,它有一个循环,每两秒钟按一次上箭头键。当我按下空格键时,循环必须被激活,当我再次按下空格键时,循环必须被停用。我现在用这个 $Space:: if GetKeyState("Space", "P") { Loop { Sleep 2000 Send {Up} if GetKeyState("Space", "P") { return } } }

我正在尝试编写一个脚本,它有一个循环,每两秒钟按一次上箭头键。当我按下空格键时,循环必须被激活,当我再次按下空格键时,循环必须被停用。我现在用这个

$Space::
if GetKeyState("Space", "P")
{
    Loop
    {
        Sleep 2000
        Send {Up}

        if GetKeyState("Space", "P")
        {
            return
        }
    }
}
由于某种原因,循环内的
if
条件不起作用,即我无法退出循环。我希望任何人都能帮助我…

使用如何


如果GetKeyState(“Space”,“p”)
当循环到达第二个循环时,您需要保留空间
让它破裂;您需要将
返回
替换为
中断

然而,我同意Gary的观点,尽管我会这样写:

; (on:=!on) reverses the value of variable 'on'
; the first press of space reverses on's value (nothing) to something (1)
; the second press reverses on's value from (1) to (0)
; when (on = 1) delay will be set to 2000, and Off when (on = 0)

space::SetTimer, Action, % (on:=!on) ? ("2000") : ("Off")

Action:
Send, {up}
Return
%开始一个表达式

?:
三值运算符
此运算符是if-else语句的简写替换。
它评估其左侧的条件以确定
其两个分支中的哪一个将成为最终结果。

例如,var:=x>y?2:3如果x大于y,则在Var中存储2;否则它会存储3个。

哦,太好了。比我的干净多了。
; (on:=!on) reverses the value of variable 'on'
; the first press of space reverses on's value (nothing) to something (1)
; the second press reverses on's value from (1) to (0)
; when (on = 1) delay will be set to 2000, and Off when (on = 0)

space::SetTimer, Action, % (on:=!on) ? ("2000") : ("Off")

Action:
Send, {up}
Return