Is there a command that exists that can simulate keypresses? I want to pipe some data to it to make it type into a GUI program for me.
2 Answers
Yes, it is xdotool.
To simulate a key press, use:
xdotool key <key>
For example, to simulate pressing F2:
xdotool key F2
To simulate pressing crtl + c:
xdotool key ctrl+c
To simulate pressing ctrl + c and then a Backspace:
xdotool key ctrl+c BackSpace
Check man xdotool to get more idea.
You might need to install the xdotool package first to use xdotool command.
-
Sweet! I'm assuming people use this for remote connections?SpecialBomb– SpecialBomb2016-03-04 21:10:54 +00:00Commented Mar 4, 2016 at 21:10
-
@SpecialBomb Depends..i use it sometimes to simulate a key inside a script..heemayl– heemayl2016-03-04 21:13:03 +00:00Commented Mar 4, 2016 at 21:13
-
Well, thanks! I'm going to use it specifically to make my own remote connection thing.SpecialBomb– SpecialBomb2016-03-04 21:15:34 +00:00Commented Mar 4, 2016 at 21:15
-
1I used it so that my computer wouldn't sleep while I was watching movies.Liam– Liam2016-03-05 00:11:05 +00:00Commented Mar 5, 2016 at 0:11
-
1What :key syntax is it to enter UP arrow key? Found it here as
xdotool key Upaskubuntu.com/q/382005/22308Nam G VU– Nam G VU2019-12-25 09:11:28 +00:00Commented Dec 25, 2019 at 9:11
Use expect (man expect)
#!/usr/bin/expect
#set timeout 10
set clientName [lindex $argv 0];
set hostName [lindex $argv 1];
set passWord [lindex $argv 2];
spawn ssh "$hostName";
expect "Password:";
send "$passWord\r";
expect "$hostName";
send "cd /apps/bin\r";
expect " bin]";
-
7
expectwon't work with GUI programs.Dmitry Grigoryev– Dmitry Grigoryev2016-03-22 16:19:44 +00:00Commented Mar 22, 2016 at 16:19