...making Linux just a little more fun!

<-- prev | next -->

Using Windows Keyboard Media Buttons In Linux

By Tom Brown

A lot of people making the move from Windows to Linux have a Windows keyboard with extra buttons along the top. Some of these buttons control the browser, while others control audio/video playback. As many have already discovered, these buttons don't work in Linux. At least, they don't by default.

The problem with these buttons is that, while they communicate with the computer via keycodes like the regular keys, these codes aren't standard ASCII. For example, the Microsoft Natural Keyboard Pro uses two keycodes for each button press. The first code is a flag to indicate that the next one will identify which button was pressed.

The process I'm going to describe is taken from KDE version 3.2. Versions prior to that lack the interface to KHotKeys that the latest one has.

Identify Your Keyboard to KDE

The first step is to make sure your keyboard is properly identified to KDE. Go to the KDE Control Center - you'll find it listed in the "KMenu" (click the icon where the "start" button would be in Windows). Once the Control Center is up and running, click the "Regional & Accessibility" (or similar - it may be called "Accessibility" in other Linux distributions - Mandrake, for example) icon in the left-hand panel, the contents of which will change. From this new list, select "Keyboard Layout". Now on the right-hand panel, go to the "Layout" tab, and make sure the "Enable keyboard layouts" check box is checked. Pull-down the "Keyboard model" combo list box, and select your keyboard. It may originally say something like "Generic 101-key PC". You'll find a lot of keyboards listed, from a lot of manufacturers, so hopefully yours will be in there. In my case, I selected "Microsoft Natural Keyboard Pro / Microsoft Internet Keyboard Pro".

Now comes the fun part. With your keyboard properly identified, KDE will now recognize those extra keyboard buttons when you press them. All you have to do is tell it how to translate them into the proper action. The technique I'm going to show you involves a KDE program called "KHotKeys". Some people recommend that an action like changing the audio volume is more properly done using the program "KMix" (a sound mixer). Unfortunately, this didn't work in my case. Every time I raised and lowered the master volume in KMix, instead of controlling the volume on my Sound Blaster Live card, it made funky changes to the tone. YMMV. If KMix works fine for you, then use it for volume control. The other reason I did it my way, and ignored those recommendations, is that I wanted all of my changes in one place.


You currently have "Keyboard Layout" selected in the KDE Control Center list on the left-hand side of the screen. Move the mouse to "KHotKeys" at the bottom of the list. If this isn't anywhere in the list, make sure you are using KDE 3.2 or later (the version of KDE you're running will appear in the right-hand panel of the Control Center when you first open it up). Click on "KHotKeys", and the right-hand panel will contain the KHotKeys configuration screen.

KHotKeys is a powerful program that allows you to define actions corresponding to certain keystrokes. It also allows you to assign actions to mouse gestures. We're interested, however, in how to assign an action to a keypress - and, by extension, to the press of one of those media buttons.

Running a Program

We'll do something simple first. Many keyboards have a button marked "Mail". We're going to define an action for that button so that it launches Mozilla's email module. First, click the button marked "New Action" at the bottom of the config screen. Under "Action Name" in the "General" tab, replace "New Action" with something easy to remember, like "Mail". Pull-down the "Action Type" list box, and select "Keyboard Shortcut -> Command/URL (simple)". Make sure the "Disable" checkbox is empty. Enter anything you want in the "Comment" box. Go to the "Keyboard Shortcut" tab, and click the mouse in the tab's only button. You'll see a dialog box. Press the "Mail" button on the keyboard, and the dialog box goes away. With my keyboard, this results in the button displaying "XF86Mail". Go to the "Command/URL Settings" tab. Enter the command line to be executed, including the full path, and any arguments. To run the mail module of Mozilla 1.6 on my machine, I used the following command:

/opt/mozilla/run-mozilla.sh /opt/mozilla/mozilla-bin -mail

Depending on what program (or shell script) you're running, the command could be more or less complicated than this. If I just wanted to run the Mozilla browser (for the "Web/Home" button, for instance), I would have used:


Using DCOP to Control a Running Program

DCOP, or Desktop Communications Protocol, is a generic way for applications to communicate with one another. You have seen this sort of thing in Windows, which uses a technology called OLE Automation (a part of "COM", or "Common Object Model"). DCOP is the method we will use to send commands to an already-running program.

From the "General" tab of KHotKeys, create a new action, and set the "Action Type" to "Keyboard Shortcut -> DCOP Call (simple)". From the "Keyboard Shortcut" tab, identify the button you want to use, just as you did with the previous example. Next, the "DCOP Call Settings" tab may seem a bit confusing if you've never worked with DCOP before. At the bottom of the tab is a button labeled "Run KDCOP". Kdcop is a GUI application that allows you to browse the callable functions exposed by all running DCOP applications in a standard tree view. Click the button to start kdcop. Start the program you want to control, locate its name in kdcop, and click the plus sign icon next to the name to expand it.

For this example, I'm going to use Jukebox (or "Juk"), an audio player that comes with many Linux distributions. When you expand the entry "juk" in kdcop, you see five entries. For this example, we want the one named "Player". Expanding this "Player" node, we see a long list of supported function calls. Now all we have to do is find the things we want to control. Let's say we want the button to start juk playing music. Scroll down the list until you see "void play()". That looks like programmer stuff, doesn't it? Don't worry, you don't have to know what it all means to make use of it. It's easy.

Copy the top tree entry (in this case, type "juk") into the "Remote application" box. Then, copy the next node entry in the tree (in this case, type "Player") into the "Remote object" box. Finally, copy the command, without the "void", or the parenthesis (in this case, type "play") into the "Called function" box. Note that all of these are case-sensitive.

Both kdcop and the KDE Control Center support drag-and-drop, so you could also click and drag "void play()" to the KHotKeys window, and drop it on any of the edit boxes. This results in "dcop juk Player play " (note the trailing space) being pasted into the edit box. This is an easy way to get the three entries you need for the "DCOP Call Settings" tab. In this example, type "juk" into the "Remote application" box, "Player" into the "Remote object" box, and "play" into the "Called function" box. Now, if you did this the way I did, and dropped everything into "Called function", deleting everything up to the word "play", then you made the same mistake I made as well. For some reason, the system adds a space to the end of the text, so the system looks for a command called "play " instead of "play", and the button won't work. Using drag-and-drop here is more trouble than it's worth, in my opinion. It would be nice if you could just drop the entry onto the tab, and have it populate all the fields for you, but that's not how it works right now.

Translating a Button into a Standard Keypress

If the application you're trying to control doesn't support DCOP, then you have to simulate the pressing of a specific key combination on a button press. For example, if you're using Mozilla 1.6, you can't use the DCOP method to control the browser. Fortunately, you can convert a button into a standard keypress that Mozilla understands. Once you have created a New Action for KHotKeys, select an "Action Type" of "Keyboard Shortcut -> Keyboard Input (simple)" from the pull-down list box in the "General" tab. From the "Keyboard Shortcut" tab, identify the button you want to use, just as you did with the examples above. On the "Keyboard Input Settings" tab, enter the keystrokes you want the button translated to. Special keys have to be spelled out. For example, to display your Home Page in Mozilla 1.6, you hold down the "Alt" key, while pressing the "Home" key. You type this into the "Keyboard input" field as (without the quotation marks, and without any spaces): "Alt+Home". Warning: this button-to-key translation doesn't care what application you're in. Think about what you're doing before you assign the keyboard input to avoid unexpected results. Note that this isn't limited to one keystroke. You could use "Hello World" if you wanted to.


[BIO] Tom has been a software developer since the early days of the Commodore 64, with such commercial classics as Prototerm-64 and Colorez-128, and has seen lots of operating systems come and go. Every one he's liked is either discontinued (OS/2) or out of business (Commodore Amiga). He currently likes Red Hat Linux, which won't be supported after April '04. As a result, we've been trying to get him to fall in love with Windows, but so far no luck.

Copyright © 2004, Tom Brown. Released under the Open Publication license unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 104 of Linux Gazette, July 2004

<-- prev | next -->