如何在 AutoHotKey 中使用热键关闭 SetTimer?

How can I toggle SetTimer off with a hotkey in AutoHotKey?

我正在尝试编写一个切换功能的脚本。

这是一个 AutoHotkey 2 脚本。

j::
    SetTimer "NewTimer", 1000
    return

k::
    SetTimer "NewTimer", Off
    return

NewTimer() {
    SendInput "NewInput"
}

按 J 应启动计时器,按 K 应停止计时器。 目前,只要我按住键,按 K 只会停止它。 如何通过按键停止计时器?

k::
SetTimer "NewTimer" , "Off"
Return

Off 必须在引号中,否则它会尝试传递变量 "Off" 的内容。持有 K 似乎暂停它的原因是因为它正在快速将周期更新为 Off 中包含的值,该值为空且错误,因此它恢复到之前的周期 1000.

或者,如果这是唯一的计时器,则仅使用 SetTimer , "Off" 也可以。

您可以考虑摘录自 the official docs for SetTimer:

If hotkey response time is crucial (such as in games) and the script contains any timers whose subroutines take longer than about 5 ms to execute, use the following command to avoid any chance of a 15 ms delay. Such a delay would otherwise happen if a hotkey is pressed at the exact moment a timer thread is in its period of uninterruptibility:

Thread, interrupt, 0  ;//Make all threads always-interruptible.