26

I want to write an AutoHotkey script which presses a key X number of times. For example, here's a script which presses Tab 10 times.

Send, {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}

While the above solution works, it's a bit unwieldy.

Is there a better solution for sending a key multiple times?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

2 Answers2

50

Try using Send {Tab 10}

Repeating or Holding Down a Key

To repeat a keystroke: Enclose in braces the name of the key followed by the number of times to repeat it. For example:

Send {DEL 4}   ; Presses the Delete key 4 times.
Send {S 30}    ; Sends 30 uppercase S characters.
Send +{TAB 4}  ; Presses Shift-Tab 4 times.

Source: AutoHotkey - Send / SendRaw / SendInput / SendPlay / SendEvent: Send Keys & Clicks


This also works with ControlSend and ControlSendRaw

ControlSend, Edit1, {Enter 5}, Untitled - Notepad
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • How to parameterize the character and the number ? So that if i press "Ctrl + op + $ + 8" on a notepad, it should print "$" totally 8 times in the same line. And "Ctrl + op + # + 5" should print the character "#" totally 5 times in the same line – Kdroid Jul 12 '20 at 16:02
  • How would you send multiple time several keys like `^+,`? – JinSnow Jul 23 '21 at 07:12
  • 1
    @JinSnow You could use a loop, like in [Avatar’s answer](https://stackoverflow.com/a/50432508/3357935) – Stevoisiak Jul 23 '21 at 14:01
  • Thanks! This works: `loop 4` `send ^+,` – JinSnow Jul 23 '21 at 17:11
8

If you want the repeat in a loop, let's say every 3 seconds:

#a::   ; Win+a
Loop, 10
{
    SendInput {Tab}
    Sleep, 3000
}
Avatar
  • 14,622
  • 9
  • 119
  • 198