xdo - do (keyboard or mouse) things in X
Posted Mon, 18 Jun 2007
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' | ./xdoIt 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") | ./xdoSeems 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