May 30, 2005

CLI Magic: Energize your console with redefined keys

Author: Nicholas Petreley and Jono Bacon

With Linux keyboard customization, you can not only redefine the action of a key: You can actually define keys to send strings of characters, which, at the console, issue commands.

This article is excerpted from the newly published book "Linux Desktop Hacks: Tips and Tools for Customizing and Optimizing Your OS."

In this example, you're going to redefine keys to control your CD-ROM drive as a CD player. If you have a plain keyboard, you can use unusual key combinations such as Ctrl-Alt-Right Arrow to perform the kind of magic you're about to explore. If you can determine the
keycodes generated by any special keys you have on your Internet or
multimedia keyboard, you can use those keys instead.

The cdtool program is really handy for controlling a CD player at the command line without a bothersome user interface. You can use another tool if you prefer, but you'll have to substitute your tool's commands for the ones defined by cdtool. I'll assume you are using cdtool to define commands to
play a CD, stop playing it, advance to the next track, and move to the
previous track.

Create a file called/etc/mykeys in which to place string definitions that represent commands. Begin by defining labels for the command strings. Add to/etc/mykeys:

string F100 = "cdplay\n"
string F101 = "cdstop\n"
string F102 = "cdplay +\n"
string F103 = "cdplay -\n"
string F104 = "eject\n"

Notice that each command string includes a trailing \n. This is the equivalent of pressing the Enter key. If you don't add the \n at the end of each string, the computer "types" the command, but doesn't execute the command until someone presses Enter.

Normal keyboards

If you have a normal keyboard with no added multimedia keys, decide
on a set of keys you want to modify. In this example, you will assign
the following keys these actions:

Keyboard command

Action taken


Play the CD


Play the next track


Play the previous track


Stop the CD


Eject the CD

Because you want Ctrl-Alt-Insert to begin playing an audio CD in your
CD drive, look for the definition for the Insert key in/etc/mykeys. That keycode is 110. Add a line below the keycode definition that makes the combination control+alt+keycode 110 execute the string
represented by F100, which is cdplay\n.

Assume you want Ctrl-Right Arrow to play the next track on a CD. Find
the definition in/etc/mykeys for the Right
Arrow key, which is keycode 106. It already has one definition
(increase to the next console). Add another definition below that so
that Ctrl-Alt-Right plays the next track on a CD (string F102).

When you are finished assigning all the F100-F104 actions to the
keys, the relevant section of your/etc/mykeys file should look like this:

keycode 103 = Up
        alt     keycode 103 = KeyboardSignal
        control alt     keycode 103 = F104
keycode 104 = Prior
        shift   keycode 104 = Scroll_Backward
keycode 105 = Left
        alt     keycode 105 = Decr_Console
        control alt     keycode 105 = F103
keycode 106 = Right
        alt     keycode 106 = Incr_Console
        control alt     keycode 106 = F102
keycode 107 = Select
keycode 108 = Down
        control alt     keycode 108 = F101
keycode 109 = Next
        shift   keycode 109 = Scroll_Forward
keycode 110 = Insert
        control alt     keycode 110 = F100

Special keyboards

I have a Logitech Elite keyboard which has multimedia keys for starting and stopping a CD player, moving forward and backward through the CD tracks, and so on. If you have a similar keyboard, you can find out what keycodes these keys generate by using the showkey command. Execute showkey, then press the keys for which you
want the keycodes. Here is a sample showkey

$ showkey
press any key (program terminates 10s after last keypress)...
keycode  28 release
keycode 165 press
keycode 165 release
keycode 163 press
keycode 163 release
keycode 164 press
keycode 164 release
keycode 166 press
keycode 166 release
keycode 171 press
keycode 171 release

When you execute the showkey command it tells you that you have 10 seconds in which to enter a keypress. If you don't send one within that time the program will terminate. Ignore the first keycode, 28 release in this list, as it represents the fact that I released the Enter key after executing showkey.

Given the order in which I pressed my special keys and the output of
showkeys, I was able to create the following

Special keyboard key


Previous track

Keycode 165

Next track

Keycode 163


Keycode 164


Keycode 166


Keycode 171

You should already have defined the strings for the special keys
F100-F104, but I'll repeat them here so that you can
see the associations more clearly. Assuming you have a Logitech Elite
keyboard with the same keycodes, the following section is what you
should add to or modify in your/etc/mykeys file:

string F100 = "cdplay\n"
string F101 = "cdstop\n"
string F102 = "cdplay +\n"
string F103 = "cdplay -\n"
string F104 = "eject\n"

keycode 163 = F102
keycode 164 = F100
keycode 165 = F103
keycode 166 = F101
keycode 167 =
keycode 168 =
keycode 169 =
keycode 170 =
keycode 171 = F104

The last thing you need to do is save your work, and then load the
new key definitions with this command:

# sudo loadkeys/etc/mykeys

Now, even if you don't have a multimedia keyboard, you can use your keyboard at a virtual text console to play and manipulate audio CDs.

The only problem with defining a special/etc/mykeys file and loading it manually is that you will lose this customization the next time you boot your Linux system. You can find and replace the default configuration file for your system, but that's not a very good way to make these changes permanent. You'll probably overwrite the modified system key bindings with a new version the next time you upgrade the package that contains the configuration file. It is best to save your custom settings in a file somewhere, such as/etc/mykeys, and load them automatically within a startup script after all the necessary startup scripts have finished. Virtually every Linux distribution gives you a method to add these sorts of extra commands. Here are some samples to show you where you would add the command to load the special key configurations.

You might notice that you don't need to use sudo to redefine the Caps Lock key when you make the modification in this file. This file is automatically executed as root, so sudo is not necessary.

You need to place the following code into each file. In most instances it needs to be placed at the end of the file, so it doesn't hurt to put it at the end for all files:

echo "keycode 58 = VoidSymbol" | loadkeys

Here are the files you need to edit for various distributions:

Fedora Core 3:/etc/rc.local
SUSE 9.1:/etc/init.d/boot.local
Mandrake 10.1:/etc/rc.local
Gentoo ~x86:/etc/conf.d/local.start

Undoing your custom keys and macros

If you want to set the behavior of your keyboard back to the
defaults, all it takes is one simple command:

$ sudo loadkeys --default

XFree86 and Xorg tend to override settings you make for console use,
so you don't usually have to undo them before you start up a graphical desktop.

WARNING: On rare occasions, something you define with
loadkeys can sneak into your graphical desktop
keyboard definitions and cause unexpected behavior. If you run into
that problem, set your keyboard configuration back to the defaults
before starting your graphical desktop.

Click Here!