如何在 AutoHotkey 中的 SendInput 命令之间添加延迟?

How do I add a delay between SendInput commands in AutoHotkey?

我的 AutoHotkey 脚本使用 SendInput which sends MouseClick 命令的速度太快,我的程序无法处理。我的脚本将发送 MouseClick 以聚焦输入字段,然后在该字段完成聚焦之前开始输入。

我试过使用 SetKeyDelay 让我的脚本 运行 慢一点,但这不适用于 SendInput

Note: SetKeyDelay is not obeyed by SendInput; there is no delay between keystrokes in that mode. This same is true for Send when SendMode Input is in effect.
Documentation for SetKeyDelay

我目前的解决方法是在每次输入后使用睡眠命令,但这不太理想。

SendMode Input
F1::
  MouseClick, left, 61, 50         ; select title field
  sleep 100                        ; artificial delay to prevent misfocused inputs

  SendInput %user_input%{Enter}    ; enter job title
  sleep 100                        ; artificial delay

  MouseClick, left, 67, 408        ; select job
  sleep 100                        ; artificial delay
Return 

理想情况下,我想要一个更优雅的解决方案,用于在每个 SendInput 命令之间添加延迟,而无需每次都手动使用睡眠命令。

如何在 AutoHotkey 中的 SendInput 命令之间添加延迟而不重复使用睡眠?

尝试使用 SendPlay instead of SendInput

这会在每次点击后延迟 100 毫秒发送文本和鼠标点击

user_input := "hello world"
SetMouseDelay 100, Play
SendPlay {Click 61,50}%user_input%{enter}{click 67,408}

来自documentation for SendPlay.

SendPlay

Note: SendPlay may have no effect at all if UAC is enabled, even if the script is running as an administrator. For more information, refer to the FAQ.

Like SendInput, SendPlay's keystrokes do not get interspersed with keystrokes typed by the user. Thus, if the user happens to type something during a SendPlay, those keystrokes are postponed until afterward.

Although SendPlay is considerably slower than SendInput, it is usually faster than the traditional SendEvent mode (even when KeyDelay is -1).

SendPlay does not use the standard settings of SetKeyDelay and SetMouseDelay. Instead, it defaults to no delay at all, which can be changed as shown in the following examples:

SetKeyDelay, 0, 10, Play  ; Note that both 0 and -1 are the same in SendPlay mode.
SetMouseDelay, 10, Play