如何在自动热键中暂停循环

问题描述 投票:0回答:2

我需要使用 f10 等键暂停空格键垃圾邮件宏,这是我的代码

c::
Loop
{
    if not GetKeyState("c", "P")
        break
    Sleep 25 ; ms
    Send {space}
}
return

我尝试在循环内外添加类似于 getkeystate 的暂停,但没有成功。

autohotkey
2个回答
1
投票
q::
Loop
{ 
Click, right,
Mousemove, 0, 110, 5, Rel
click, left
Mousemove,   350, -473, 5, rel
click, left
Mousemove,  -350,  363, 5, rel
}
return

#p::Pause,Toggle

https://autohotkey.com/board/topic/95308-endless-loop-with-hotkey-pause/


1
投票

我总是做这样的事情:

#MaxThreadsPerHotkey 2 ; Allows 2 "instances" of the hotkey to exist simultaneously
c::
Toggle := !Toggle
While Toggle {
    ; Do whatever you need to do here
}
Return

这里的另一个优点是只需记住一个热键。按一次开始无限循环。再按一次即可停止。


自从最初发布以来,我添加了

#MaxThreadsPerHotkey
指令,如果您的脚本在其他地方没有该指令,则可能需要该指令。默认值为 1,这将防止注意到新按下的热键,因为第一次触发永远不会自然结束。任何大于 1 的值都足以满足此方法的目的。

如果您使用

#Warn
,请注意:您需要在调用此函数之前定义
Toggle
(例如,
Toggle := False
)以避免出现警告消息;放置它的好地方是脚本顶部的“自动执行部分”(即第一个热键/热字符串定义或返回/退出语句之前的所有内容)。或者您可以使用替代函数,例如下面的示例。 这是相同的东西,但作为一个函数,它允许您在其他地方安全地重用

Toggle

变量名称:

#MaxThreadsPerHotkey 2 ; Allows 2 "instances" of the hotkey to exist simultaneously
c::
FunctionHotkey() {
    Static Toggle := False
    Toggle := !Toggle
    While Toggle {
        ; Do whatever you need to do here
    }
}

函数的名称并不重要。此外,以这种方式定义和使用时不需要 
Return

语句;如果需要更多信息,请参阅

有关此用法的文档

© www.soinside.com 2019 - 2024. All rights reserved.