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
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.)