使用GetKeyState()和循环 [英] Using GetKeyState() and loops

查看:109
本文介绍了使用GetKeyState()和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个具有循环的脚本,其中每两秒钟按下一次向上箭头键.当我按下空格键时,必须激活该循环,而当我再次按下它时,必须将其禁用.我现在正在使用它.

I'm trying to write a script that has a loop in which the upper arrow key is pressed every two seconds. The loop must be activated when I press the spacebar and deactivated when I press it again. I'm now using this.

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

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

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

For some reason, the if condition inside the loop doesn't work, i.e. I can't get out of the loop. I hope anyone can help me out...

推荐答案

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

You wouldn't need the first if GetKeyState("Space", "P")
and you would need to be holding space when the loop got to the second one
for it to break; and you would need to replace the return with break.

尽管我会这样写,但我同意加里:

However I agree with Gary, although I would write it like this:

; (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中;否则x大于y.否则将存储3.

?:
Ternary operator
This operator is a shorthand replacement for the if-else statement.
It evaluates the condition on its left side to determine
which of its two branches will become the final result.
For example, var := x>y ? 2 : 3 stores 2 in Var if x is greater than y; otherwise it stores 3.

这篇关于使用GetKeyState()和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆