4

I'm using xdotool to automate running commands, opening new tabs, etc.

The thing is when doing it on the current window, I've to specifically sleep for some time or use xdotool keyup Return before doing anything or else xdotool won't press the enter key.

kartik@kartikpc:~/junk/xdotool$ cat automate 
#!/bin/bash

# Release the Return key
# xdotool keyup Return
# Or sleep 1

xdotool type --delay 1 --clearmodifiers "clear"
xdotool key --clearmodifiers Return

kartik@kartikpc:~/junk/xdotool$ source automate 
clearkartik@kartik-lappy:~/junk/xdotool$ clear

What I've read from very few sources is

% sleep 1; xdotool type "$(printf "hello\nworld\n")" (the sleep is for letting me release my actual 'return' key before typing)

I understand that the 'return' key is pressed when I specifically invoke my script by pressing 'Enter' on the keyboard. But why isn't it released automatically?

Even when xdotool is typing stuff using xdotool type shouldn't the 'return' key release till that time, or every letter should have been going line after line, instead of coming on the same line

Kartik Anand
  • 4,513
  • 5
  • 41
  • 72
  • This might be a better question for the tool's developer, not StackOverflow. The best we can do is speculate. – Mr. Llama Dec 04 '15 at 16:16
  • I've asked this here because there are a lot of questions regarding `xdotool`. Secondly I thought 'return' key not being released immediately is some how related to terminals or other os concept? – Kartik Anand Dec 04 '15 at 16:18

1 Answers1

5

The issue has more to do with the state of the keyboard itself than any special OS concepts. If key is only said to be "pressed" when it transitions from the "up" to "down" states.

When an application tries to send a keypress, it will send a keydown followed by a keyup. If the key is already in the "down" state, sending a keydown won't register as a key press because the key's state didn't transition from "up" to "down", it merely stayed in the "down" state. (Sending a keydown while already in a "down" state is equivalent to simply holding the key down, not pressing it another time.)

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • So it take that much time for it to transition from "down" to "up" state because of which I've to specifically wait for it or "sleep"? – Kartik Anand Dec 04 '15 at 17:18
  • Not quite. The sleep is to make sure you (the user) have enough time to release the enter key before `xdotool` attempts to send a keypress. In theory the key can be pressed dozens of times per second, it's the user releasing the key that's the limiting factor. Using the `xdotool keyup Return` will be faster and more reliable than simply sleeping. – Mr. Llama Dec 04 '15 at 17:45