xdotool - Why sleep before running commands?

你。 提交于 2019-12-12 13:58:18

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/34092604/xdotool-why-sleep-before-running-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!