Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending keyboard input to a program from command-line

How do you send keyboard input to a program?

That is, under a Linux GUI, is there a good manual (programmable) way, or tool, of simulating keyboard input on a running program on Linux, so that I can send from the command-line, e.g., "Control-T" to a Firefox process and "echo 'hello'\n" to a Gnome-Terminal process without actually focusing on each of those processes and typing in directly?

like image 590
OTZ Avatar asked Sep 25 '10 04:09

OTZ


People also ask

How do I use SendKeys in CMD?

Enter keyboard characters literally as they are to be sent. The codes and functions are defined in the tables below. In the table below, keyboard keys are presented with the code that should be entered in the SendKeys window. To repeat keys, add a number of repeats to the key code as follows: {LEFT 42} or {h 10}.


2 Answers

xdotool does have a way of sending keystrokes if limited to a focused window:

WID=`xdotool search "Mozilla Firefox" | head -1`
xdotool windowactivate $WID
xdotool key ctrl+l
like image 97
OTZ Avatar answered Nov 10 '22 17:11

OTZ


It's an old topic, but one still may be looking for this, someone mentioned here solution where window must be activated when using xdotool. However you can specify window and even use xdotool to find it. Here is example I tried to accomplish, change it as you need.

xdotool key --window $(xdotool search --name "Spotify (Premium |Free )?- Linux Preview" | head -n1) ctrl+KP_Down
like image 23
Chlorek Avatar answered Nov 10 '22 15:11

Chlorek