使用GetKeyState()和循环

时间:2012-03-08 19:20:48

标签: autohotkey

我正在尝试编写一个循环,其中每两秒按一次上箭头键。按下空格键时必须激活循环,​​再按一次时停用循环。我现在正在使用它。

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

        if GetKeyState("Space", "P")
        {
            return
        }
    }
}

由于某种原因,循环内的if条件不起作用,即我无法退出循环。我希望有人可以帮助我...

2 个答案:

答案 0 :(得分:1)

您不需要第一个if GetKeyState("Space", "P")
当循环到达第二个时,你需要保持空间 它要打破;您需要将return替换为break

但我同意加里,虽然我会这样写:

; (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

%开始表达。

来自http://l.autohotkey.net/docs/Variables.htm

?:
三元运营商
此运算符是if-else语句的简写替代 它评估左侧的状况以确定
它的两个分支中的哪一个将成为最终结果 例如,var:= x> y?如果x大于y,则2:3将2存储在Var中;否则它会存储3。

答案 1 :(得分:0)

如何使用SetTimer

; Create timer.
SetTimer, SendUp, 2000 

; Set timer to 'Off' at start of script.
SetTimer, SendUp, Off
TimerEnabled := False

; When Space is pressed toggle the state of the timer.
$Space::
    If TimerEnabled
        {
        SetTimer, SendUp, Off
        TimerEnabled := False
        }
    Else
        {
        SetTimer, SendUp, On
        TimerEnabled := True
        }

; Label called by timer to send {Up} key.
SendUp:
    Send, {Up}
return