Search this site


Metadata

Articles

Projects

Presentations

xdo - do (keyboard or mouse) things in X

Yesterday, I talked about macros. I spent some time coding today and I now have a tool that will let you execute raw keyboard and mouse input into X using the XTEST extension.

The primary example I used was focusing firefox's URL bar without the mouse. The sequence was this: Switch to Desktop 2 (I press Alt+2), focus firefox's URL bar (using control+l) and clear it.

The result is a simple tool I'm tentatively calling 'xdo'. You can download the source here. Compile instructions are at the top of the file.

The top of xdo.c details the implemented commands, so let's cut to an example:

% echo 'key alt+2; sleep 1; key ctrl+l; key BackSpace' | ./xdo
It does exactly what you think. The 'sleep' command has values in milliseconds, and is only necessary to slow down so that events can propgate fast enough (window focus changes, etc).

Another reasonable example would be to say "firefox, open a new tab and load the URL in my clipboard":

# My clipboard contains a valid url, say, "http://www.google.com/"
(echo "key alt+2; sleep 1; key ctrl+l; key BackSpace;"
 echo "move 55 55; sleep 1; click 2; key Return") | ./xdo
Seems complex, but look at what's really happening: Go to desktop 2, focus urlbar, hit backspace (clearing it), move the mouse cursor to 55,55 (a point inside the urlbar for me), hit middle mouse button to paste.

Change "ctrl+l" to ctrl+k (unix firefox) to focus the "Search" box instead, and change the 'move' command to cursor over the search box to paste instead, and suddenly you can bind a simple keystroke to search for whatever is in your X clipboard. Useful.

One of the neater features is that you can 'type' text:

% echo 'type echo hello there; key Return' | ./xdo
echo hello there
% echo hello there
hello there

7 responses to 'xdo - do (keyboard or mouse) things in X'

Showing last 7 comments... (Click here to view all comments)

Assume Rhino wrote at Tue Jun 19 02:59:03 2007...
Sounds like a great tool! One question, can it simulate mouse clicks? Say I want to show the right click menu (context menu) in Finder or another application by pressing e.g. Ctrl+x. Is this possible?

Jordan Sissel wrote at Tue Jun 19 04:15:25 2007...
Yes. With 'click 3' you will send a right click. ('2' would be middle mouse).

I'm working on a lightweight interface to calling functions provided by xdo and other tools. Think OS X's quicksilver tool but for X.

Spoonman wrote at Tue Jun 19 10:35:23 2007...
Freakin' A!  I've been looking for something like this for ages.  Between this and wmctrl, I'm all set finally!  It just amazes me that in all of the years Linux has been around that this is the first time someone's built something like this...

Spoonman wrote at Tue Jun 19 10:36:13 2007...
Freakin' A!  I've been looking for something like this for ages.  Between this and wmctrl, I'm all set finally!  It just amazes me that in all of the years Linux has been around that this is the first time someone's built something like this...

Sefa Denizoğlu wrote at Sun Dec 16 10:57:53 2007...
It seems that this is what I look for. I need a pseudo key entry or mouse click so that my screen and ACPID doesn't interrupt when I just want to leave my laptop for a long download or smth else.. perhaps I can make a regular task out of it via cron?

Vermind wrote at Fri Nov 7 10:28:18 2008...
What about the xmacro package? http://xmacro.sourceforge.net/

emg wrote at Sat Aug 15 19:47:35 2009...
can u help me to set hach bot,auto full comp,and cool prefect all anything ..thanks ..coz i need to test tq,den send me da botting


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: