June 23, 2011, 12:51 p.m.
posted by lambda
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.
You 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.
Using the "Other" Alt Key
You 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/mykeys
Now 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_10
The 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_10
Save 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/mykeys
Now 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.
Deep-Six the Caps Lock Key
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.
How to Interpret Keycode Files
When you dump the list of keycodes and their various permutations (which is what happens when you combine them with some other key or key combination, such as Alt, Alt-Ctrl, etc.) with the dumpkeys --full-table command, it prints out 16 columns in an order determined by the numeric value of the first five modifying keys: 0, 1, 2, 4, and 8. Here is a list of the numeric values of all the key modifiers, including the ones that aren't used:
Notice that you can add any combination of modifying keys and still get a unique column number. Ctrl-Shift is 4+1. Although it is possible to use ShiftL, ShiftR, CtrlL, and CtrlR (the left and right Shift and Ctrl keys), these keys aren't assigned by default. That's why you get only 16 key combinations, each represented in a column of a row of combinations for each keycode. If you print out a complete list of keycodes and their functions, you should get 16 columns. Each column would represent, in order:
You don't see all 16 columns when you use the --keys-only switch. You see a subset of columns defined by the first line of the output of the command. The first line of the output of the dumpkeys --keys-only command is keymaps 0-2,4-6,8-9,12. This means the dumpkeys --keys-only command displays only the following nine columns: modifiers 0, 1, 2, 4, 5, 6, 8, 9, and 12.
Indeed, the --keys-only command might not even print all nine columns, because it does not print columns that are not assigned an action. If nothing happens when you press certain key combinations (such as Shift-Ctrl), the command does not print that column. (The official definition of an inactive key is VoidSymbol).
For example, look at the definition line for keycode 59 in the output of our example file:
keycode 59 = F1 F13 Console_13 F25 alt keycode 59 = Console_1 control alt keycode 59 = Console_1
The first column shows F1, which is the key without any modifiers. The second column is F13, which is Shift-F1. The third column is Console_13, which is Altgr-F1. Because the columns are defined as 0-2,4-6, etc., the output skips modifier 3, and the next column we can expect to see is the key with modifier 4, or Ctrl-F1. This produces F25. The next column we should see is modifier 5, which is Shift-Ctrl-F1. But no action is defined for this combination. The fact that nothing happens when you press Shift-Ctrl-F1 would normally be represented by the definition VoidSymbol in that column, but the --keys-only switch avoids printing out VoidSymbol.
As a result, notice that the keycode table format changes for the rest of the keycode combination definitions. The rest of the possible actions for F1 are spelled out in separate lines. That's why the output skips to a new line that says Alt keycode 59 = Console_1 (this would normally appear in column 8), followed by another line that says Control-Alt-keycode 59 = Console_1 (normally column 12).
If you want to see a file that has a column definition for every possible key modification combination from 0-15, including all the VoidSymbols, issue the command dumpkeys --full-table.