【问题标题】:Using GetKeyState() and loops使用 GetKeyState() 和循环
【发布时间】:2023-04-02 19:36:01
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

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

    不过我同意 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
    

    % 开始一个表达式。

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

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

    【讨论】:

    • 哦,很好。比我的干净得多。
    【解决方案2】:

    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
    

    【讨论】:

      猜你喜欢
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 2014-08-21
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多