AHK如何让ExitApp立即生效?

How to get ExitApp to take effect immediately in AHK?

如果我 运行 下面的代码,如果我点击 ^+q 它不会停止输入数字 1-100。只有在它完成后脚本才会退出。有没有办法让脚本停止,即使它正在发送击键?

^j::
ArrayCount := 100

Loop % ArrayCount
{
Send, %A_index%
}
return

^+q::ExitApp  ; Exit script with Escape key

您的代码有 2 个问题。

Send在模拟输入时释放修改键,以这种方式在循环中使用它会干扰 autohotkey 的热键检测。如果您同时按下 3 个按钮,您仍然可以激活 ^+q,但是使用没有修饰符的热键会更容易,例如 Escape 键。这也是你的评论说你在做什么

^+q::ExitApp ; Exit script with Escape key

作为奖励,它会修复您的评论和代码之间的差异 ;)。

第二个问题是,如果您使用 SendInput,执行 Send 命令的循环将很快结束,到 ExitApp 执行完所有号码已经发送(即使您还没有看到效果)。在 SendEvent 的情况下,还有一些其他问题会阻止您在循环中执行其他线程(不知道是什么原因导致的,可能是错误)。

要解决它你需要添加Sleep。在我的系统中,Sleep 1 运行良好。您可以尝试不同的数字和发送模式,直到获得所需的效果(您也可以尝试 0-1.

完整代码:

^j::
ArrayCount := 100
Loop % ArrayCount
{
    Send %A_index%
    Sleep 1 ; experiment with how long to sleep
}
return

Escape::ExitApp  ; Exit script with Escape key