5

manpage of xdotool says

-- delay milliseconds Delay between keystrokes. Default is 12ms.

I thought that key string will type each character in the string once, and --delay is for pause between typing two consecutive characters. So why do the following first two commands not show typing anything, while the third types twice, and the last types many times? Thanks.

$ xdotool key 9
$ xdotool key --delay 2 9
$ xdotool key --delay 1000 9
99$ xdotool key --delay 10000 9
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999^C

I am using Lubuntu 18.04 and

$ xdotool -v
xdotool version 3.20160805.1
Tim
  • 25,177
  • I can't reproduce the first or second on 14.04 or 18.04, and I can't reproduce the third on 18.04. In any case, it seems like a bug. – wjandrea Dec 06 '18 at 00:24
  • BTW it's easier to test xdotool commands in terminal if you add a short delay first, like sleep 0.1 && xdotool key 9 – wjandrea Dec 06 '18 at 00:25
  • Thanks. I added my version. What does --delay mean ideally? – Tim Dec 06 '18 at 00:28
  • Ideally it's the delay between consecutive keystrokes, just like you said. – wjandrea Dec 06 '18 at 00:33
  • key --delay <n> 9 should type the key 9 once, whatever value n has, and thus only a single digit is displayed, correct? – Tim Dec 06 '18 at 00:36
  • That's correct. BTW I just noticed xdotool type is also affected. E.g. trying sleep 0.1 && xdotool type --delay 2000 hello types hheelllloo – wjandrea Dec 06 '18 at 00:39
  • Why "it's easier to test xdotool commands in terminal if you add a short delay first"? – Tim Dec 06 '18 at 00:47
  • Indeed with longer delays, it is as if the key autorepeat is kicking into action. If longer delays are needed, use sleep. – vanadium Dec 06 '18 at 10:50
  • @Tim A short delay prevents any of your keystrokes from conflicting with the xdotool keystrokes, but there's not much chance of conflicts in this case. – wjandrea Dec 10 '18 at 14:32
  • @wjandrea By "conflict", do you mean the same as the case when I type a sequence of characters from keyboard, the computer may accept the characters in a different order? (This happens when my RAM or CPU is being heavily used.) – Tim Dec 10 '18 at 14:48
  • @Tim By "conflict" I mean for example if you hold down Shift and run xdotool key m at the same time, you will get a capital M instead of a small m. – wjandrea Dec 10 '18 at 14:50
  • @wjandrea Thanks. That is different from the case (also see https://askubuntu.com/questions/1099816/why-are-the-characters-that-i-typed-missing-or-out-of-order). I also remember the out or order problem sometimes happens when I use xdotool. – Tim Dec 10 '18 at 14:51

1 Answers1

6

The manual indeed says

Delay between keystrokes. Default is 12ms.

This leads to expect that between each (simulated) press and release of a key, there will be a delay by the set value. The option, however, behaves rather as if the key is pressed during the period indicated by the delay, with no such delay in between different key presses. When the delay is long enough, autorepeat kicks in and the character is repeated.

To obtain the desired behavior where there is a pause between keypresses, one should rather use the sleep command:

xdotool sleep 1 key 9

9 will be pressed after one second. Two consecutive keypresses one second apart can be issued by

xdotool sleep 1 key 9 sleep 1 key 2

Alternatively, when working with bash scripts, a delay can be introduced using the bash sleep command between invocations of xdotool.

#!/bin/sh
sleep 1
xdotool key 9
sleep 1
xdotool key 2
vanadium
  • 88,010