Hack 11. Energize Your Console with Macro Music Magic
Redefine keys to issue commands at the command 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 MagicFirst, 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" 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. 2.3.2. Normal KeyboardsIf 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:
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 2.3.3. Special KeyboardsI 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: $ 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 table:
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. If you want to have your computer automatically reload the new key definitions at startup, see the sidebar Keep Your Custom Keys Intact.
2.3.4. Undoing Your Custom Keys and MacrosIf 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.
|