photo
Jordan Sissel
geek

Fri, 06 Jun 2008

xdotool - fake keyboard/mouse input

What is xdotool?

This tool lets you programatically (or manually) simulate keyboard input and mouse activity, move and resize windows, etc. It does this using X11's XTEST extension and other Xlib functions.

Mailing list

The xdotool users mailing list is: xdotool-users@googlegroups.com

I'll be announcing new versions on this mailing list. Additionally, if you want help or want to contribute patches to xdotool, the mailing list is a good place to go.

Prerequisites

  • xlib (pkg-config xlib) - Standard Xlib library
  • xtst (pkg-config xtest) - XTEST library

Download

xdotool-20080606.tar.gz

Looking for older versions? Check the archive.

Build Instructions

'make xdotool'

If this fails, and the error is becuase 'pkg-config' is not found, you should try the alternative Makefile: Makefile.no-pkg-config. This is done by doing 'make -f Makefile.no-pkg-config xdotool'

How to use it

Basic usage is: xdotool <cmd> <args>

Read the documentation here

Example: focus the firefox url bar

WID=`xdotool search "Mozilla Firefox" | head -1`
xdotool windowfocus $WID
xdotool key ctrl+l

Example: Resize all visible gnome-terminal windows

WIDS=`xdotool search --onlyvisible --name "gnome-terminal"`
for id in $WIDS; do
  xdotool windowsize $id 500 500
done

Comments: 52 (view comments)

Permalink: /projects/xdotool/main
posted at: 03:46


52 responses to 'xdotool - fake keyboard/mouse input'

Nico posted at Tue Jul 17 11:31:39 2007...
Hi, I don't have pkg-config and the alternative Makefile Makefile.no-pkg-config doesn't seem to be included in any of the archives. Can you tell me where I can find it?

Jordan Sissel posted at Tue Jul 17 12:23:23 2007...

dilly posted at Sat Jul 21 18:49:51 2007...
Hi,
great program!
exactly what I was looking for :)
I'm running ubuntu, and whenever I execute xdotool, it gives me a xdo_type error for every argument given..

like this :
"%./xdotool type 'hello'
helloxdo_type reported an error
%hello"

(wether I type 'hello' or "hello" doesn't change a thing)
any idea on what's wrong? :-/

Jordan Sissel posted at Sun Jul 22 04:46:17 2007...
@dilly

Thanks for the bug report! I confirmed this on my ubuntu box. It's due to me forgetting to put return statements at the end of a few funtions ;)

Fixed, and I've uploaded a new release.

Lee Pumphret posted at Sun Jul 22 17:59:14 2007...
Thanks for such a great tool!

A quick question, any possibility of adding an option to get the id of the currently focused window?

I'm working on an application that will let me send events to X applications based on actions from a input events (in particular, a 3DConnexion Space Navigator 6D mouse). Between this and xte, I've got just about everything I need.

I was getting the "xdo_type reported an error" message as well (hence using xte) hopefully this new release will take care of it.

Thanks,
-Lee

Lee Pumphret posted at Sat Jul 28 23:00:16 2007...
Hi Jordan,

I made a modification to xdotool to get the current focused window so it can be restored from scripts.

xdotool getwindowfocus

I'm not up to date on SVN, my C is really rusty but everything seems to work fine. If you're interested, the modified source is here.

http://www.leeland.net /lib/leeland/xdotool-20070722-lee.tar.gz

Thanks,
-Lee

Jordan Sissel posted at Mon Jul 30 00:41:27 2007...
@Lee, thanks! Great idea :)

I looked at your changes and added them to my copy with some tiny modifications.

I put a new release up.

Lee Pumphret posted at Mon Jul 30 03:55:26 2007...
Thanks Jordan!
Like i said, my C is rusty, outside of some perl XS stuff, the last time I used C with any regularity was a decade ago (Watcom anyone?). And X11, as far as dev, this is my first foray, though I have a feeling I'll be learning a bit in the near future.

Now I just have to look and see what I did wrong ;)

Jordan Sissel posted at Mon Jul 30 16:03:16 2007...
Your code wasn't bad at all :)

The only changes I made were purely style changes (spacing) and stuff to my own preferences.

Specifically, the xdo_window_get_focus takes a window pointer now instead of returning the window. It returns success/error code and modifies the window pointer with the currently-focused window.

Lee Pumphret posted at Mon Jul 30 16:34:52 2007...
Thanks Jordan,

You know what else might be useful? Being able to retrieve the location and size of a given window.

Jordan Sissel posted at Mon Jul 30 19:11:01 2007...
I explicitly didn't implement this. There are a few tools that already query information about a given window: xwininfo and xprop.

Here's an example of how to get info on the currently focused window:

% ./xdotool getwindowfocus | xargs xwininfo -id

xwininfo: Window id: 0x400000e "[0] xdotool getwindowfocus | xargs xwinin... - kenya2(/usr/home/jls/projects/xdotool)  "

  Absolute upper-left X:  603
  Absolute upper-left Y:  322
  Relative upper-left X:  3
  Relative upper-left Y:  18
... more stuff here snipped ...


Does this help?

Lee Pumphret posted at Mon Jul 30 21:07:40 2007...
Yes it does. Though I'm thinking down the line the combination of both would be a handy Perl XS module.

Jordan Sissel posted at Mon Jul 30 21:21:58 2007...
There's an Xlib Perl module (X11::Protocol?) that may do what you want here.

Lee Pumphret posted at Tue Jul 31 02:47:40 2007...
Actually I started with the X11::* modules, but for most of them the docs are lacking to say the least. That's how I ended up here :)

Maybe now that I've grokked thing a bit better, I should take another look.

sven posted at Wed Sep 5 15:54:37 2007...
Hi, great program, though I am searching for some way to also specify the xserver I want this key event to be executed, not just to send keys from within my x session.

Reason is: I just got a new mobile that is WLAN-capable, I also have an asterisk server running at home which my mobile connnects perfectly to with SIP. And now, I was thinking of using

my mobile as a remote control ;-) !

by calling my asterisk server and then sending keytrokes to my x session by simply executing a command like yours from within asterisk server.

Quite weird idea, I know, but i already have the asterisk server running fo real phone calls, so why not use it for something like this, too :-) ?

could you extend your program to specifyling an xserver or do you know some program that could do this trick, havent found one yet.


thanks and great work!

Jordan Sissel posted at Wed Sep 5 16:33:34 2007...
@sven,

You can use the 'DISPLAY' environment variable to achieve this. For example, if you have your server 'foo' that you want to control, you can ssh in and do:

DISPLAY=:0 ./xdotool [stuff]

on 'foo' and it will talk to the local X server (again, on foo)

davija posted at Tue Sep 11 01:20:59 2007...
I've been looking for something like this for quite a while.  I see this as a must have for any type of gui programming and have been surpised to find so little on it.

Are you intending on releasing this as public domain, gpl, or other?

Thanks, this is very col.

phil posted at Fri Sep 14 01:51:57 2007...
you might try the make command first, and get something like this:

xdo.h:8:22: error: X11/XLib.h: No such file or directory. 

If you get that, you're missing the XLib library as I was.  Installation instructions below.

if you lack the XTEST library, the error might look like:
/usr/bin/ld: cannot find -lXtst
collect2: ld returned 1 exit status

For those who swing by and are using Debian or Ubuntu, here is a touch more detail about the commands I used to get it built:

sudo apt-get install libx11-dev
sudo apt-get install libxtst-dev
make xdotool

If you're new to debian (as I am) you'll probably want to know about how to find out what packages are available.  The command for that is

apt-cache search <name>

by using this, i was able to pick out the two packages mentioned above, which Jordan relies on.

Thank you Jordan!

Cory posted at Sun Sep 30 22:56:03 2007...
You are my hero!

Thank you for such a lovely utility. :-)

Luca Clivio posted at Sat Oct 6 01:31:14 2007...
Hello all.
Thanks for this great piece of software.
I would like to propose a modification to the function xdo_window_raise included in xdo.c for making it work with Gnome2. Its purpose is to make an arbitrary window stay on top of all the others.
This code compile and works with me. I am using Ubuntu feisty.

int xdo_window_raise(xdo_t *xdo, int wid) {
  int ret;
//  ret = XRaiseWindow(xdo->xdpy, wid);
//  XFlush(xdo->xdpy);

  Display *disp = xdo->xdpy;

  XEvent ev;
  memset(&ev,0,sizeof(ev));
  ev.type=ClientMessage;
  ev.xclient.type = ClientMessage;
  ev.xclient.message_type = InternAtom(disp,  "_NET_WM_STATE", 0);
  ev.xclient.display=disp;
  ev.xclient.window=wid;
  ev.xclient.format=32;
  ev.xclient.data.l[0]= 1;
  ev.xclient.data.l[1]=XInternAtom(disp, "_NET_WM_STATE_STAYS_ON_TOP", 0); // *_STAYS_ON_TOP is probably KDE-specific
  XLockDisplay(disp);
  XSendEvent(disp, XDefaultRootWindow(disp), 0, SubstructureRedirectMask | SubstructureNotifyMask, &ev);

  ev.xclient.data.l[1]=XInternAtom(disp, "_NET_WM_STATE_ABOVE", 0); //to work with some non-KDE WMs we should use _NET_WM_STATE_ABOVE
  XSendEvent(disp, XDefaultRootWindow(disp), 0, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
  XUnlockDisplay(disp);

  return _is_success("XRaiseWindow", ret);
}

A call to window_raise and then to window_focus can make any wanted window active and on top, with focus.
I got the informations on this URL:
http://www.nabble.com/Always-on-top,-aka-Keep-Above-Others-(a-solution-for-Windows-and-Linux)-t3995328.html

Luca

Daniel Kahn Gillmor posted at Sat Dec 29 12:43:26 2007...
This is a very intriguing tool.  Are you interested in seeing it in debian?  I'd be up for maintaining the debian package, if you're willing to license it under a license that debian can accept.  I couldn't find any licensing info in the tarball.

Whatever you decide about the licensing, thanks for writing this up and publishing it.  There's good ideas in here, and it's kind of hard to believe we've been without a tool this fundamental! (i couldn't find any older one, anyway)

Jordan Sissel posted at Sun Dec 30 00:03:15 2007...
My release script didn't include the COPYRIGHT file (bsd license), oops. I fixed that and put up a new version. No changes except adding the file with a license.

nicola posted at Sun Jan 6 10:41:47 2008...
Thanks for the tool, I just started thinking on writing a limited tool for personal use when I come across your good work. Thanks again.

bob loblaw posted at Sun Jan 6 23:17:56 2008...
a great alternative to irxevent, let's me run irexec while still being able to utilize irxevent-like functionality via xdotool; better, more general, than irxevent of course. thanks!

Vito posted at Tue Jan 8 12:09:38 2008...
Getting strange results with 'type' subcommand under KDE konsole:

$ xdotool type 'this is rather big string to type'

konsole shows:
this is rather biigg  stttrnoype

Some synchronization problems?
(BTW, the same command works perfectly from xterm)

Lado posted at Thu Jan 10 07:41:54 2008...
Thank you for very useful tool. I used it in a script to automaticaly fill password in wined Lotus Notes. The tool can be used like autoit on windows.

Is there a way to press control buttons: up, down, left, right?

Jordan Sissel posted at Thu Jan 10 13:32:24 2008...
You can tell it any key to use with 'xdotool key'. For example 'xdotool
key Up' will send the up arrow.

Alfons posted at Sun Jan 13 07:16:54 2008...
Hi Jordan

I would love to send KeyCodes from /usr/share/X11/XKeysymDB to my Xorg.

But i don't seem to do it right. It is possible with xdotool or any other tool you know?

I have no luck with:
./xdotool key XF86Book
No such key 'XF86Book'. Ignoring it.
No such key 'XF86Book'. Ignoring it.

nor with XK_Find from /usr/include/X11/keysymdef.h
./xdotool key "XK_Find"
(symbol) No such key name 'XK_Find'. Ignoring it.
(symbol) No such key name 'XK_Find'. Ignoring it.

Thanks for any hint,
Alfons

Adam posted at Mon Feb 4 17:20:37 2008...
Great work!  Extremely useful.

learner lea posted at Thu Feb 14 02:24:37 2008...
thank you for this great tool. That's what i am looking for.There is a little trouble.When i use Redhat 9.0 in VM,the 'xdotool mousemove' doesn't work.Please email me about it in your sparetime

Beau posted at Sat Mar 1 06:11:23 2008...
Hi!

This is exactly the tool I'm looking for!
But I can't get it to work :(

When I run a second command the windowfocus goes back to my console so the fake key event ends op in my console window.

e.g.:
beau@hal ~ $ xdotool windowfocus 10485766
beau@hal ~ $ xdotool getwindowfocus
33554437

The windowfocus command works though, the window gets highlighted on the taskbar.

I'm running KDE 3.5.9 and Xorg-x11 7.3

What could I be doing wrong?

Jordan Sissel posted at Sat Mar 1 14:59:51 2008...
I haven't tested with KDE. It's likely there needs to be some extra message sent so that KDE knows to focus it.

I'll see what I can do. Thanks for the report ;)

J. Kleis posted at Tue Mar 18 14:17:11 2008...
Hi, has someone write a script, which paste the actual Date. I've tried it, but i collapsed.

J. Kleis

Jordan Sissel posted at Tue Mar 18 14:26:54 2008...
This should work:
xdotool type "`date`"

(It works for me)

Pille posted at Thu Mar 20 20:38:19 2008...
Hi! I was very happy to find your tool ;) But I've a problem with it on Fedora 8. I installed everything, but "using" it with "xdotool" in terminal brings me a "command not found" return.
When I try to reinstall it, everything returns is "xdotool is already up-to-date" How can I get it to work?

Nakul posted at Sat Mar 22 18:30:56 2008...
thanks for the tool dude.
@pille: try running ./xdotool

pan posted at Mon Mar 24 04:23:59 2008...
How can I get window Id of all open X windows? Also, how can I get Id of previously focused window than current cursor positioned window?

jet posted at Wed Apr 16 17:43:26 2008...
Great work, I was looking for something like this with python xlib .. I'm really happy that I found this little tool, It would be nice addition to python if anyone knows how to port it

jet posted at Wed Apr 16 19:06:12 2008...
I just found that there is an older tool called wit which does similar things .. you should take a look

Randy Robertson posted at Wed Apr 23 03:39:10 2008...
Brilliant.  Exactly what I was looking for.  There are at least 5 problems I can fix immediately with this now.

It took me forever to find this, partially because terms like "X11" "click" "mouse event" and "inject" are so generic that Google gets lost in the mess.  If you don't mind, I'm going to make a post on my site about this, and use some english sentences to describe what it does so that hopefully others can find it by searching the same terms that were failing me for so long.

markus posted at Mon May 12 15:39:28 2008...
This is a very important tool.

I wonder if there is a chance that the Xorg maintainers could include it and make it part of the official xorg? I for one very much would favour such a decision.

Mortelrdv posted at Sun Jun 1 06:20:32 2008...
Hello

i try xdotool-20080601.tar.gz
if i do "xdotool keyup e", xev show "KeyPress event" and "KeyRelease event".
is possible to simulate only "KeyRelease event" ?

Thank

Jordan Sissel posted at Sun Jun 1 11:45:50 2008...
Hmm, I see the same thing. I'll look into it.

Jordan Sissel posted at Sun Jun 1 12:27:46 2008...
Looks like the previous version did this too.

When I send a keydown, then a keyup, 'keyup' only causes a KeyRelease event. However, if the key isn't down in the first place, it sends both KeyPress and KeyRelease. This happens outside of my code and likely outside of my control.

Assuming it's out of my control, there may be a way around it: when 'keyup' is sent we could try to snag the KeyPress event that is sent before another client receives it, doing this seems kludgy and unreliable.

If you really need only KeyRelease to show up when 'keyup' is given, even if there the key you are sending is not currently pressed, let me know, and I'll see what I can do.

Mortelrdv posted at Sun Jun 1 13:07:01 2008...
I thought that I needed it but i don't, at the moment i use it for the mouse (i don't have the same problem with mouse button).
But one day I shall need it (i'm sure).
keydown => it is truly keydown
keyup => it must be truly keyup

Thank
PS : i'm french, my english is bad

null posted at Mon Jun 23 09:36:21 2008...
sorry...i dun know how to use it please teach..

SomeGuy posted at Mon Jun 23 18:13:24 2008...
Hi,
nice programm.
Can someone please tell me how to call the "tab" key?
i tried this:
xdotool key TAB  (also tab, Tab etc)

another question,
how to have an "@" symbol typed?
xdotool type "someone@somewhere.com"
did typed all but @ .

/edit
found a way to get the @ working with:
xdotool type "someone"
xdotool key Mode_switch+q
xdotool type "somewhere.com"

but if there is an easier way please let me know.

thanks

yoshi posted at Tue Jun 24 04:50:24 2008...
i got this.. if placed in cron.. y?

Error: Can't open display: 0.0
Failed creating new xdo instance
Error: Can't open display: 0.0
Failed creating new xdo instance
Error: Can't open display: 0.0
Failed creating new xdo instance
Error: Can't open display: 0.0
Failed creating new xdo instance

Jordan Sissel posted at Tue Jun 24 06:19:50 2008...
@SomeGuy:

The following presses the 'tab' key for me:
% xdotool key Tab

To your second question, it looks like there's some issue with typing shifted keys.

This lets me type '@':
% xdotool key shift+2
also:
% xdotool key shift+at

Ideally, you should only have to do 'xdotool key at' but this doesn't work. I'll work on a fix, thanks for the report :)

SomeGuy posted at Tue Jun 24 07:26:06 2008...
@Jordan

thanks for reply.
xdotool key Tab
now works, dont know why it didnt worked yesterday,guess i made an mistake ;)

im working with a german keyboard, where @ is normaly Alt_R+q (and Alt_R = Mode_switch)(shift+2 = " , shift+at = Q and at = q)
So i guess for my german keyboard the
xdotool key at
will even if fixed not be working for me?

Dont know if its important, the key works, but i got error messages in Konsole:
xdo_keysequence reported an error for string 'Mode_switch+q'
xdo_keysequence reported an error for string 'Tab'
xdo_keysequence reported an error for string 'Return'
but as long the keys are working i shouldnt be afraid of it, right?

btw: a forum for this cool program would be nice :)

Jordan Sissel posted at Tue Jun 24 13:02:32 2008...
There's a user group noted at the top of the page. You can access it on the web by going to:
http://groups.google.com/group/xdotool-users

I'll do my best to make 'xdotool key at' work as expected. I'll post an update here and probably on the mailing list when the next version is up.

Jarrod posted at Fri Jul 4 00:00:57 2008...
Thanks for making such a great tool public.

Just thought I'd pass on a little unexpected feature I found: on line 549 of xdo.c (version 20080606) ret is not being initialized to 0. On my system this results in xdo_keysequence reporting an error when in fact everything has (as far as I can tell) worked fine.

Cheers.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment:


Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials

Tag Cloud

Calendar

< June 2008
SuMoTuWeThFrSa
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
2930     

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati