views:

129

answers:

3

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?

A: 

This will work for linux:

Expect

This will work on windows

VBScript/JScript:

var process = WScript.CreateObject("WScript.Shell");  
process.Run("i-use-gui-command-here --with -a --few args");  
WScript.Sleep(1000);  
process.AppActivate(process.ProcessID);  
process.SendKeys("{TAB}{TAB}{TAB}{ENTER}");  

from: Oleksandr Gavenko

Guillaume Massé
Isn't Expect for command-line automation, and not for GUI apps? Windows part is not really necessary as the question targets X apps on Linux.
OTZ
A: 

I found this two programs xmacro and xremote you may take a look, but its seems that they not well documented. And I also find this utility xvkbd found in answer to this question sending-keycode-to-xorg-wine-with-bash-script.

jcubic
+2  A: 

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
OTZ
List of key symbols you can find in `/usr/include/X11/keysymdef.h` file.
jcubic