April 26, 2011, 2:56 a.m.
posted by lambda
Heat Up Your Keyboard with Hotkeys
Activate those special Internet and multimedia keys on your keyboard.
Hotkeys is a bit antique these days, so it doesn't support all the modern keyboards. But if you are willing to put in some effort, you can get a decent experience from Hotkeys, especially if your keyboard is supported. If your keyboard is not supported, it might take more work than it is worth to you, and you might want to skip ahead to the more modern LinEAK [Hack #29] .
First, you must install Hotkeys; you can use your distributions package manager or download and compile it yourself. If your Linux distribution doesn't have a package available, you can find RPM packages for various distributions at http://rpm.pbone.net/index.php3?stat=3&search=hotkeys&srodzaj=3, or you can search http://rpmfind.net. Just as an example, if you use Debian, get the packages with this command (logged in as root):
# apt-get install hotkeys xosd-bin
By installing xosd-bin at the same time you can take advantage of one of the better features of Hotkeys: the attractive use of on-screen display (Figure). If you compile hotkeys yourself, be sure to also install the XOSD and XOSD development programs [Hack #26] to take advantage of this feature.
The Hotkeys on-screen volume display
Start the process of configuring Hotkeys for your keyboard by copying the default configuration to your personal configuration file. The default configuration file is usually saved as /etc/hotkeys.conf. You want to create a copy of this file in your home directory, and personalize it to work with your keyboard and perform the actions you want performed when you press the special keys on your keyboard:
$ mkdir ~/.hotkeys $ cp /etc/hotkeys.conf ~/.hotkeys
Now look to see which keyboards are supported by default:
$ hotkeys -L Supported keyboards: (with corresponding options to --kbd-list or -l) mx2500 - Memorex MX2500 Keyboard kbp8993 - Chicony KBP-8993 keyboard sk2500 - Fujitsu/Logitech/Trust SK2500 Keyboard / Liteon-ak2500 sk2505 - SK-2505 Keyboard sk2800c - SK-2800C ibook - iBook Internal Keyboard mx1998 - Memorex MX1998 Keyboard sk2501a - Silitek SK5210A Keyboard msnatpro - Microsoft Natural Keyboard Pro msnet - Microsoft Internet Keyboard logitech-cfo - Logitech Cordless Freedom Optical Keyboard polypix - Polypix Keyboard sk7100 - Silitek SK7100 Keyboard itouch - Logitech Cordless iTouch/Internet/Cordless Desktop hp5181 - HP 5181 Internet Keyboard msnetpro - Microsoft Internet Pro Keyboard acerwl - Acer Wireless Keyboard btc9000 - BTC 9000 orktekusb - ORKTEK USB Hub/keyboard kb9930 - IBM Rapid Access II Keyboard mck800 - Process MCK-800 kb9963 - Compaq KB-9963 keyboard pb5140w - Packerd Bell Model 5140W mx3000 - Memorex MX3000 Keyboard sk9925 - Silitek SK-9925 USB Keyboard
This isn't a terribly impressive list, but if your keyboard is there, you're all set. If your keyboard isn't listed, you can use one of the existing Hotkey keyboard definitions as a template to create a new keyboard definition. For example, to create a definition for the Logitech Elite keyboard, log in as root and run these commands:
# cd /usr/share/hotkeys # cp itouch.def lelite.def
Now you need to find out what the keycodes are for the Logitech Elite keyboard and adjust the definitions in the file. This is the hardest part of the process. The best way to discover what keycodes your special keys generate is to execute the following command within an X terminal:
You will see a small window appear with a box inside it. Click the window to make sure it's active. Now press the various special keys on your keyboard. As you press each key, information about that key should appear in the terminal from which you launched xev. If you press a special key and nothing appears on the X terminal where you started xev, you won't be able to assign that key to an action. Also, if you see a result that looks something like this, you probably won't be able to assign that key to an action:
KeymapNotify event, serial 30, synthetic NO, window 0x0, keys: 4294967222 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ButtonRelease event, serial 30, synthetic NO, window 0x1400001, root 0xb6, subw 0x1400002, time 2414137, (35,22), root:(1053,797), state 0x200, button 2, same_screen YES
However, if you see a result that looks something like the following, you're in luck:
KeyPress event, serial 27, synthetic NO, window 0x1400001, root 0xb6, subw 0x1400002, time 31223, (44,18), root:(1085,771), state 0x0, keycode 162 (keysym 0x0, NoSymbol), same_screen YES, XLookupString gives 0 bytes: XmbLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 30, synthetic NO, window 0x1400001, root 0xb6, subw 0x1400002, time 31447, (44,18), root:(1085,771), state 0x0, keycode 162 (keysym 0x0, NoSymbol), same_screen YES, XLookupString gives 0 bytes:
In this example, your keypress generated a keycode of 162. Jot down that keycode and make a note of which key generated it. In the case of the Logitech Elite keyboard, the Play key generates keycode 162.
Given all I could find out about the Logitech Elite keyboard, here is an abbreviated version of the /usr/share/hotkeys/lelite.def definition file I re-created from the original itouch.def file:
<?xml version="1.0"?> <definition> <config model="Logitech Elite Keyboard"> <Favorites keycode="230"/> <Shopping keycode="148"/> <PrevTrack keycode="144"/> <Play keycode="162"/> <Stop keycode="164"/> <NextTrack keycode="153"/> <VolUp keycode="176" adj="2"/> <VolDown keycode="174" adj="2"/> <Mute keycode="160"/> <Email keycode="236"/> <Search keycode="136"/> <Sleep keycode="223"/> <Go keycode="233"/> <!-- Media keycode="129"/ --> <userdef keycode="129" command="/usr/bin/eject">Eject</userdef> <!-- My Home keycode="130 --> <userdef keycode="130" command="/usr/bin/xfe">Home</userdef> <!-- Go keycode="233" --> <userdef keycode="233" command="/usr/bin/firefox \ -remote openURL( )"></userdef> </config> <contributor> <name>nicholas petreley</name> <email>nicholas at petreley dot com</email> </contributor> </definition>
Notice that some definitions begin with userdef. Hotkeys is very strict about the names you define for the available keys on the keyboard. Hotkeys simply refuses to recognize some key names, and therefore you cannot associate those names with a keycode. You can get around the problem by creating a user-defined key definition. In the case of the Logitech Elite keyboard, there is a Media key, a My Home key, and a Go key (among others undefined by Hotkeys). I added these keys along with their functions to the /usr/share/hotkeys/lelite.def file using the Hotkeys userdef command:
<!-- Media keycode="129"/ --> <userdef keycode="129" command="/usr/bin/eject">Eject</userdef> <!-- My Home keycode="130 --> <userdef keycode="130" command="/usr/bin/xfe">Home</userdef> <!-- Go keycode="233" --> <userdef keycode="233" command="/usr/bin/firefox \ -remote openURL( )"></userdef>
Now you're ready to edit the ~/.hotkeys/hotkeys.conf file to define the actions Hotkeys will take when you press certain special keys. Here is a sample configuration:
### Specify the default keyboard (without the .def extension) so you ### don't need to specify -t every time Kbd=lelite CDROM=/dev/cdroms/cdrom0 # PrevTrack=xmms --rew # Play=xmms --play-pause # Stop=xmms --stop # Pause=xmms --pause # NextTrack=xmms --fwd # Rewind= WebBrowser=firefox Email=thunderbird Calculator=xcalc FileManager=gentoo MyComputer=gentoo MyDocuments=gentoo Favorites=firefox Shell=aterm ScreenSaver=xscreensaver-command -activate NewsReader=thunderbird -news Communities=firefox -remote 'openURL(http://slashdot.org,new-tab)' Search=firefox -remote 'openURL(http://www.google.com,new-tab)' Idea=firefox -remote 'openURL(http://sourceforge.net,new-tab)' Shopping=firefox -remote 'openURL(http://thinkgeek.com,new-tab)' Go=firefox -remote 'openURL( )' Print=lp osd_font=-adobe-helvetica-bold-r-normal-*-*-240-*-*-p-*-*-* osd_color=LawnGreen osd_timeout=3 osd_position=bottom osd_offset=25
Some of the actions are simple. The Email key starts the Thunderbird program, so all you need in the file is the line Email=thunderbird. Other definitions require more complex actions, such as opening a new tab in a web browser (if one is already running) and pointing it to a specific web page.
The key definition is Kbd=lelite, which tells Hotkeys which keyboard definition file to use. In this case, it instructs Hotkeys to use /usr/share/hotkeys/lelite.def.
I confess that most of the keys listed in the configuration file do not exist on the Logitech Elite keyboard, so the definitions are useless. But it's sufficient for my tastes as it enables my keyboard to control the volume, play audio CDs, launch Firefox, and open Google in a browser tab. If you have a keyboard that is officially supported, you can do a lot more.