Hack 11. Energize Your Console with Macro Music Magic
line.You can exploit the power of
the preceding keyboard
customization technique to a much greater degree than just redefining
the action of a key. You can actually define keys to send strings of
characters, which, at the console, means issuing commands.In this example, you're going to redefine keys to
control your CD-ROM as a CD player. Even if you have a plain
keyboard, you can simply 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.
2.3.1. Defining the Magic
First, you want to create a file called
/etc/mykeys, or add to your existing
/etc/mykeys file if you are combining this hack
with [Hack #10] . You will place in
/etc/mykeys string definitions that represent
commands. 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.Assume you are using cdtool to define commands to
play a CD, stop playing it, advance to the next track, move to the
previous track, etc. First, define labels for the command strings.
Here is what you add to /etc/mykeys:
string F100 = "cdplay\n"Notice that each command string includes a trailing
string F101 = "cdstop\n"
string F102 = "cdplay +\n"
string F103 = "cdplay -\n"
string F104 = "eject\n"
\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.
2.3.2. 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 |
|---|---|
Ctrl-Alt-Insert | Play the CD. |
Ctrl-Alt-Right | Play the next track. |
Ctrl-Alt-Left | Play the previous track. |
Ctrl-Alt-Down | Stop the CD. |
Ctrl-Alt-Up | Eject the CD. |
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
2.3.3. Special Keyboards
I have a Logitech Elite keyboard. It
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. Then, execute
showkey, and then press the keys for which you
want the keycodes. Here is a sample showkey
session:
$ showkeyWhen you execute the showkey command it tells you
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
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
table:
Special keyboard key | Keycode |
|---|---|
Previous track | Keycode 165 |
Next track | Keycode 163 |
Play | Keycode 164 |
Stop | Keycode 166 |
Eject | Keycode 171 |
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"The last thing you need to do is save your work, and then load the
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
new key definitions with this command:
# sudo loadkeys /etc/mykeysNow, 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. If you want to have your computer automatically
reload the
new key definitions at startup, see the sidebar Keep Your Custom Keys Intact.
Keep Your Custom Keys IntactThe 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 I've used 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: loadkeys /etc/mykeysHere are the files you need to edit for various distributions:Fedora Core 3: /etc/rc.localDebian: /etc/rc.bootSUSE 9.1: /etc/init.d/boot.localMandrake 10.1: /etc/rc.localGentoo ~x86: /etc/conf.d/local.start |
2.3.4. 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 --defaultXFree86 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.
|
