Get Slick With xbindkeys

by Pete Kelly (critter)

PCLinuxOS comes with a little talked about utility called xbindkeys, which is pre-installed. What it does is to 'bind' a key or key sequence to perform a function under the X windowing system, e.g. KDE. You may wonder why this is useful when systems like KDE and Gnome already have the ability to assign short-cuts to keys, and you can click on the icon of a .desktop file to launch almost any installed application. This is true, but xbindkeys takes this a step further.

With this little utility, you can perform any function recognized by the system with a simple key sequence. Add to that the ability to recognize most of those 'extra' keys that appear on multimedia keyboards, to modify any key press by the simultaneous pressing of the shift, control, alt and super (Windows) keys and more. Not only is the key press recognized, but the key release is seen as a different event. Something else that makes this utility so flexible is the ability to recognize and act upon mouse button presses with key presses, even when using those usually ignored additional mouse buttons.

Of course, you have to set it up to perform your particular magic. This is quite easy to do, even though you do need to briefly open a command line terminal. You will need to create or edit a configuration file, and xbindkeys will create a default one for you to get you started. But thanks to the PCLinuxOS developers, you don't even need to do that. They have already put one in your home directory.

The configuration file is a simple text file that can be edited with any text editor, such as kwrite. The default file supplied for you is a hidden file (the name begins with a period) in your home directory, named .xbindkeysrc. It is fairly long, as it contains a lot of helpful introductory text, but once you are familiar with the application, you can streamline the file by deleting most of this.

Open the file and scroll down until you find these lines:

# Examples of commands:

"xbindkeys_show"
control+shift + q

Any line that begins with a '#' is ignored by the program.

The text inside the quotes is the command to execute, and the line below is the sequence of keys to execute that command. What this pair of lines does is to execute the command "/usr/bin/xbindkeys_show" which is a script which will list the currently recognized commands. To try it out, click on an empty part of the desktop, hold down the control and shift keys and press q. You will be presented with a window like this:

pic

Those two lines, the command and the key sequence are all that are needed.

Why did I tell you to click on an empty part of the desktop? You will all ready have many shortcuts pre-defined by KDE or other applications, and you don't want to conflict with those. Clicking on the desktop gives it the "focus," and it is unlikely that there is a Control + Shift + Q shortcut defined there. When defining new shortcuts, for this or any application, you should consider the possibility of creating a conflict.

Look at the next two commands in the screen-shot

"xterm"
m:0x4 + c:41
Control + f

and

"xterm"
m:0x4 + b:2 (mouse)

The command is the same in both cases.

In the first case, m:0x4 + c:41 means modifier key number 4 plus character key number 41.

A modifier key is something like control, alt or shift and character 41 (in my locale, yours may vary) is the letter f. The next line repeats this in an alternative, more readable format but not all keys can be represented in this friendlier format.

The second case performs the same function, but using the control key. We know this from the previous example, and button number 2 on the mouse.

The mouse buttons are easy to assign. The left, middle and right buttons correspond to b:1, b:2 and b:3, respectively. Other buttons can be easily determined by experimentation. The keys and modifier keys need a different approach, and the programmer has included a simple way to discover any key press combination, even most of those "additional" keys. This is where we need the command line, but don't worry. This is a very simple procedure.

Open a terminal and type:

xbindkeys -k

A small window will appear on the desktop. The only purpose of this window is to "steal" the focus. Anything you type now will be directed to this window, although it won't appear there. The window will close and what you typed will be reported to its parent – the command line of the terminal window. If you press Control + Alt + W, you will see this:

pic

Where you see "(scheme function)" you will enter in your configuration file the actual command that you want the key press to perform. Under that, add either one of the two lines below. For example, if you want the key press Control + Alt + W to launch OpenOffice Writer, then in your configuration file you would enter either:

"openoffice.org3 -writer"
Control+Shift + w

or

"openoffice.org3 -writer"
m:0x1c + c:25

If you type xbindkeys -mk at the prompt, then the little window won't close after one key sequence, but will keep on echoing the codes of pressed keys to the terminal until you press q. This is useful when first setting things up.

Since version 1.8.3, the version that is currently in the PCLinuxOS repositories, a listening loop has been added to automatically detect changes in the configuration file. But if you have problems with that, or you have an earlier version and your newly added short-cut isn't seen, then executing the following line at the command prompt should resolve the matter.

killall -HUP xbindkeys

So now that you know how to find the key codes, you will need to know the command format. For anybody familiar with the command line, this should not present a problem. But for everybody else, an easy way is to right click on the menu button at the left of the panel, and select "Menu Editor" from the box that appears. Then simply locate the application that you want to launch, highlight it and copy what appears in the "Command" box on the right. You may get more text than you need, but the application will run. If it complains, then you can go back and edit out the extra info from the configuration file.

Another way is to type the application name at the command prompt, followed by --help. This invariably lists various options that can be passed to the application to modify its behavior. For instance, suppose you are playing some music through Amarok and the phone rings, so you want to pause the music. The Amarok short-cut for this is the space bar. But if Amarok has been minimized to the task bar, then this doesn't work. Typing amarok –help at the prompt gives the information that we need. If we add -t or --play-pause after the command amarok, the the state will toggle between playing and paused. These two lines:

"amarok –play-pause"
Control+Mod2 + Pause

added to the configuration file will pause playing music or restart it, if paused by pressing the control key and the pause key. I used the longer -play-pause option rather than -t, as is shows better what the short-cut does, which may be helpful in a few months time. But -t works just as well.

Now you can unclutter your desktop, make use of extra keys and buttons that have been lying idle. And, you can impress your friends with your programmed shortcuts.