View Full Version : [ubuntu] How to send keyboard input to a window

February 10th, 2011, 08:43 PM
I want to create a script to send input to an already open window from the command line.
The reason that I want to do this is to automatically control a game running on a emulator screen.
So, for example, I want to send commands like "up" "up" "w" "s" ...
Do someone have any idea on how can I do this?

February 10th, 2011, 11:24 PM
You could use "xdotool" for that, in combination with "wmctrl", both are named as such in the official repos.

I use similar scripts to send keys to mplayer, to control fullscreen and mute. Actually I use "xsendkeycode" for that, but would I have to write them now, I would do it "xdotool", it's easier.

This is the one to control fullscreen, adapted to "xdotool":


mplayerid=`xwininfo -name MPlayer |grep 'Window id:' |cut -d" " -f4`
if [ -n "$mplayerid" ]; then
wmctrl -i -a "$mplayerid"
wmctrl -a SMPlayer
xdotool key fFor "up" you have to use cap case: "Up".
You can lookup each keys "name" by running "xev" in a terminal, and then pressing the respective key.

If you know the exact name of the window and there is only one window containing the specified name, you can just use

wmctrl -a NAMElike in the above example with SMPlayer.

February 14th, 2011, 07:19 PM
Thanks for your response. It really works fine for me.

I've created a document with the script that I have done to control my game.

https://docs.google.com/document/pub?id=17_OKZSl7gEaI9XZrkJqXa5UE0_ajoaSz_pYuBRv0Ug w

Now, with this script, I can control a game running inside PCSX emulator.

February 14th, 2011, 08:47 PM
I suppose you don't need the if/then conditions at all, I just had it in there because I switched between plain MPlayer and SMPlayer that time, I just now removed it as well, it's faster without the xwininfo query.

This should be sufficient for you:

#! /bin/bash
function Move {
wmctrl -a PCSX;
xdotool key "$1";
sleep 0.05;
What does this actually?

while [ true ]
Move Right
Move Left
Move Right
Move Left

February 14th, 2011, 09:31 PM
I can make my character play around the scene alone, with this code.
I will try your suggestions.