3
votes

I have an AutoHotkey script using SendInput which sends MouseClick commands too quickly for my program to handle. My script will send a MouseClick to focus an input field, then start typing before the field finishes focusing.

I've tried using SetKeyDelay to make my script run a bit slower, but this doesn't work with 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

My current workaround is to use sleep commands after each input, but this is less than ideal.

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 

Ideally I would like a more elegant solution for adding a delay between each SendInput command without manually using a sleep command each time.

How can I add a delay between SendInput commands in AutoHotkey without repeatedly using sleep?

1

1 Answers

2
votes

Try using SendPlay instead of SendInput.

This sends text and mouse clicks with a 100ms delay following each click

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

From the 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