【问题标题】:How to interrupt a loop with a hotkey如何使用热键中断循环
【发布时间】:2013-11-23 04:39:34
【问题描述】:

这应该很容易! I've checked out this thread,但没用。

我似乎无法使用热键跳出循环。这个想法是您可以使用单个热键启动和停止循环过程。

似乎timeron 的值一旦开始就永远不会进入循环。

这是一个示例脚本:

#singleinstance force

timeron = 0
return

!f7::
    if(timeron){
        timeron = 0
        msgbox Okay, the loop is off.
    }else{
        timeron = 1 ;if this is not set to one, the loop will not begin
        msgbox Turning on the loop.
        gosub, STARTLOOPING
    }
RETURN


STARTLOOPING:
    ;do this over and over
    loop{
        if(!timeron)
            break   
        ;now wait for the right length of time before continuing the loop
        msgbox, The loop yet continues....
        Sleep, 5000
        if(!timeron)
            break
    }
RETURN

我在这里错过了什么?

【问题讨论】:

    标签: loops autohotkey


    【解决方案1】:

    由于您的 !F7 永远不会结束,第二次按 !F7 将被忽略。 默认情况下,每个热键一次只允许一个线程。

    添加

    #MaxThreadsPerHotkey 2
    

    作为脚本的第二行,第二次按 !F7 可以停用循环。

    【讨论】:

    • 我认为我可以添加一行来影响线程 - 我搜索了文档,但答案对我来说并不明显!谢谢!
    【解决方案2】:

    为什么不使用计时器呢?它们允许您的脚本在计时器运行之间执行其他操作,从而允许热键中断它们:

    timeron := false
    Exit
    !F7::
        if(timeron) {
            timeron := false
            SetTimer, MyTimer, Off
        } else {
            timeron := true
            ; Call GoSub once if you want the subroutine
            ; to fire immediately at the beginning
            GoSub, MyTimer
            ; Then let the timer repeat it
            SetTimer, MyTimer, 5000
        }
    return
    
    MyTimer:
        Msgbox, Looping like crazy!
    return
    

    您始终可以用计时器替换循环的功能。如果您有某种 for 循环/计数器,则可以改用全局变量。

    【讨论】:

    • MCL 这是一个很好的答案。但是,我标记了另一个,因为它可以在我不必过多编辑脚本的情况下工作。但我也会考虑这种方法。谢谢!
    • 其实我想我会在我的脚本中使用这个。
    • 我同意 MCL 的答案更好——这就是我自己编写循环的方式......我的回答只是对您的问题的直接回答,而不是试图优化您的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多