Hack 10. Redefine Keyboard Actions
This hack describes one of many ways you can customize the default behavior of keys and key combinations for your keyboard. If you just want to make your special function keys on your fancy Internet and multimedia keyboard work, take a look at [Hack #28] and [Hack #29] . But if you want to fine-tune how keystrokes affect virtual text consoles, this is the place for you. 2.2.1. Virtual ConsolesYou might recall that people used to attach several text-based terminals to minicomputers and mainframes so that many people could access the computer at once. Each person could log in to the computer and work on his separate text console, because he had his own text-based terminal connected to the main computer. Linux simulates that kind of behavior with a single terminal and several virtual consoles. Even if your Linux-based computer starts up automatically with a graphical login screen, you can get to the first virtual text console by pressing the key combination Ctrl-Alt-F1. If you want, you can log in and work, using text-based programs. If you press Alt-F2 from here (or Ctrl-Alt-F2), Linux takes you to another virtual console with another login prompt. This simulates another separate terminal hooked up to the computer. Alt-F3 takes you to yet another virtual console and login prompt.
2.2.2. Using the "Other" Alt KeyYou might have noticed by now that you can switch from one text console to another only by using the Alt key on the left side of the keyboard. It won't work if you use the Alt key on the right side of the keyboard with F1, F2, or any other function key. This is not an accident. Linux is configured such that the Alt key on the right side of the keyboard plus the F1 key will take you to virtual console 13. The problem is that very few people use as many as 13 virtual consoles, and most Linux distributions don't even enable more than 10 or 11 virtual consoles by default. Because you don't need to use the Alt key on the right side to switch to nonactive virtual consoles, you might as well use that Alt key in the same way the left Alt key worksto take you to virtual console 1, 2, 3, and so on. If you learn how to alter the way the keyboard is configured so that you can use the right Alt key to switch between virtual consoles, you will begin to understand how to make other useful changes in how your keyboard functions. Here's how to make the change. Dump the current default key settings to a file with the following command (you can log in as root, but a properly configured sudo should give you the right to dump the file to the /etc directory): $ sudo dumpkeys --keys-only >
/etc/mykeysNow open the file (as root, preferably with sudo) with your favorite editor and locate the following section: keycode 59 = F1 F13 Console_13 F25
alt keycode 59 = Console_1
control alt keycode 59 = Console_1
keycode 60 = F2 F14 Console_14 F26
alt keycode 60 = Console_2
control alt keycode 60 = Console_2
keycode 61 = F3 F15 Console_15 F27
alt keycode 61 = Console_3
control alt keycode 61 = Console_3
keycode 62 = F4 F16 Console_16 F28
alt keycode 62 = Console_4
control alt keycode 62 = Console_4
keycode 63 = F5 F17 Console_17 F29
alt keycode 63 = Console_5
control alt keycode 63 = Console_5
keycode 64 = F6 F18 Console_18 F30
alt keycode 64 = Console_6
control alt keycode 64 = Console_6
keycode 65 = F7 F19 Console_19 F31
alt keycode 65 = Console_7
control alt keycode 65 = Console_7
keycode 66 = F8 F20 Console_20 F32
alt keycode 66 = Console_8
control alt keycode 66 = Console_8
keycode 67 = F9 F21 Console_21 F33
alt keycode 67 = Console_9
control alt keycode 67 = Console_9
keycode 68 = F10 F22 Console_22 F34
alt keycode 68 = Console_10
control alt keycode 68 = Console_10The first line in the section that is relevant to the changes we want to make starts with the keycode definition, which is keycode 59 =. A number of columns follow the equals sign, and each column represents what is produced when you combine a modifier (such as Shift, Ctrl, etc.) with that keycode 59. F1 appears in the first column; so, when you press F1 alone (to produce keycode 59), you get F1 with no modifiers (such as Shift, Ctrl, etc.). The second column tells you what you get when you press Shift-F1, and that result is the key F13. The third column tells you what you get when you press Altgr (the Alt key on the right side of the keyboard)-F1, and that takes you to Console_13.
You want to change this key combination to take you to Console_1 rather than Console_13. All you have to do is change the definition from Console_13 to Console_1. Change Console_14 to Console_2, and so on. Edit each starting line for every keycode definition so that the previous section looks like the following: keycode 59 = F1 F13 Console_1 F25
alt keycode 59 = Console_1
control alt keycode 59 = Console_1
keycode 60 = F2 F14 Console_2 F26
alt keycode 60 = Console_2
control alt keycode 60 = Console_2
keycode 61 = F3 F15 Console_3 F27
alt keycode 61 = Console_3
control alt keycode 61 = Console_3
keycode 62 = F4 F16 Console_4 F28
alt keycode 62 = Console_4
control alt keycode 62 = Console_4
keycode 63 = F5 F17 Console_5 F29
alt keycode 63 = Console_5
control alt keycode 63 = Console_5
keycode 64 = F6 F18 Console_6 F30
alt keycode 64 = Console_6
control alt keycode 64 = Console_6
keycode 65 = F7 F19 Console_7 F31
alt keycode 65 = Console_7
control alt keycode 65 = Console_7
keycode 66 = F8 F20 Console_8 F32
alt keycode 66 = Console_8
control alt keycode 66 = Console_8
keycode 67 = F9 F21 Console_9 F33
alt keycode 67 = Console_9
control alt keycode 67 = Console_9
keycode 68 = F10 F22 Console_10 F34
alt keycode 68 = Console_10
control alt keycode 68 = Console_10Save your modifications to the file /etc/mykeys. You're not quite done. You still have to load this new set of definitions into the system before the modifications will work. You do that with the following command: $ sudo loadkeys
/etc/mykeysNow you can use either the left or right Alt key (plus a function key such as F1) to switch between the most frequently used virtual consoles. The downside, if there is one, is that you can no longer get to Console_13 or higher by pressing Altgr-F1, Altgr-F2, etc. In most cases, however, Linux does not activate consoles above Console_11, so you'll never miss the lost capability. If you want to have your computer automatically reload the new key definitions at startup, see the sidebar Keep Your Custom Keys Intact. 2.2.3. Deep-Six the Caps Lock KeyThis section describes another thing you can accomplish by changing the keycode definitions. I hate the Caps Lock key. I am always inadvertently hitting the key, after which I type a full command at the console in caps before I realize what I've done. Naturally, the command does not work, because Linux/Unix is case-sensitive. Although you can redefine the key in your /etc/mykeys file with your favorite editor (which would be a good idea if you also implement the previous hack), here are two tricks you can use without having to edit any files. This first trick simply disables the Caps Lock key for consoles so that it has no function: $ sudo echo "keycode 58 = VoidSymbol" | loadkeys Some people like to convert the Caps Lock key into a Ctrl key. Here is the simple command to do that: $ sudo echo "keycode 58 = Control" | loadkeys Again, if you want to have your computer make this change automatically, see the sidebar Keep Your Custom Keys Intact for instructions. |