Consistent Keyboard Configuration

Eliminate inconsistent behavior from your keyboard by following the instructions in this article.
Linux Kernel

First, the kernel's keyboard translation tables will be redefined. Since these tables are not used directly by the X Window System, use one of the virtual consoles, not a window manager. Log in as root or use su. Different distributions of Linux may load the translation tables in different ways. To determine which keyboard translation table is in use, type:

# find /etc -type f | xargs grep loadkeys

You should see output something like:

/etc/init.d/boot:  loadkeys \
        /usr/lib/kbd/keytables/us.map
which would indicate that the U.S. translation table is in use.

Assuming the U.S. translation table is in use, enter the following commands to make a copy of it:

# cd /usr/lib/kbd/keytables
# cp us.map custom.map

The format of keyboard table files is given in keytables(5).

Now edit custom.map using any text editor. Find the following lines:

keycode 14 = Delete           Delete
        alt     keycode  14 = Meta_Delete

These lines specify that when keynumber 14 is pressed (the BACKSPACE key), send a DELETE to the system, and when ALT-BACKSPACE is pressed, send a Meta_DELETE. To find out the keynumber of any key, use the showkey command.

To make the BACKSPACE key conform to the design decisions, change these lines to read:

keycode  14 = BackSpace        BackSpace
         alt     keycode  14 = Meta_BackSpace

However, a delete key is also needed, so replace the following line:

keycode 111 = Remove
with these two lines:
keycode 111 = Delete           Delete
        alt             keycode 111 = Meta_Delete
Keynumber 111 is the DELETE key on the editing keypad, just below the INSERT key.

Now, to swap Caps_Lock with the left CTRL key, redefine keycodes 29 and 58 as follows:

keycode  29 = Caps_Lock    # Left Control key.
keycode  58 = Control      # Caps Lock key.

Configuring the numeric keypad presents a special challenge. In us.map, several of the keys transmit the same escape sequences as an editing key. This makes it impossible, for example, for a program to distinguish between the PAGE UP key on the editing keypad and the 9/Pg Up key on the numeric keypad. Furthermore, the NUM LOCK, /, *, - and + keys do not even transmit escape sequences.

To alleviate these problems, the virtual console's numeric keypad will be configured somewhat like a VT100 numeric keypad. Since xterm already emulates a VT102, this will save work by making the virtual consoles more compatible with xterm. Note that this technique could cause incompatibilities with software programs that expect the keys to behave as defined by us.map. If this becomes a problem, the keys can always be switched back.

To be able to configure the keys of the numeric keypad independently, they will have to be changed into function keys. Since the kernel supports up to 246 function keys, F1 through F246, this is not a problem. Redefine the following keycodes as shown:

keycode  55 = F112    # Numeric keypad *.
keycode  69 = F110    # NumLock.
keycode  71 = F107    # Numeric keypad 7.
keycode  72 = F108    # Numeric keypad 8.
keycode  73 = F109    # Numeric keypad 9.
keycode  74 = F113    # Numeric keypad -.
keycode  75 = F104    # Numeric keypad 4.
keycode  76 = F105    # Numeric keypad 5.
keycode  77 = F106    # Numeric keypad 6.
keycode  78 = F114    # Numeric keypad +.
keycode  79 = F101    # Numeric keypad 1.
keycode  80 = F102    # Numeric keypad 2.
keycode  81 = F103    # Numeric keypad 3.
keycode  82 = F100    # Numeric keypad 0.
keycode  83 = F116    # Numeric keypad ..
keycode  96 = F115    # Numeric keypad Enter.
keycode  98 = F111    # Numeric keypad /.

Furthermore, it is necessary to define the escape sequences that these keys transmit, so add these lines to the end of the file:

string F100 = "\\033Op"
string F101 = "\\033Oq"
string F102 = "\\033Or"
string F103 = "\\033Os"
string F104 = "\\033Ot"
string F105 = "\\033Ou"
string F106 = "\\033Ov"
string F107 = "\\033Ow"
string F108 = "\\033Ox"
string F109 = "\\033Oy"
string F110 = "\\033OP"
string F111 = "\\033Oo"
string F112 = "\\033Oj"
string F113 = "\\033Om"
string F114 = "\\033Ok"
string F115 = "\\033OM"
string F116 = "\\033On"
"\\033" is the octal representation of ESCAPE. These are the same escape sequences that will be transmitted by these keys when running xterm, after following the remaining steps.

Although function keys F6 through F12 are compatible with xterm, F1 through F5 are not. To fix this, add these lines:

string F1 = "\\033[11~"
string F2 = "\\033[12~"
string F3 = "\\033[13~"
string F4 = "\\033[14~"
string F5 = "\\033[15~"

It is recommended that the following keycodes be defined so the keysyms (the names following the equals signs) will match the keycaps. However, this will not change the escape sequences transmitted, since these keysyms are only synonyms for the original keysyms:

keycode 102 = Home      # Was Find.
keycode 104 = PageUp    # Was Prior.
keycode 107 = Enc       # Was Select.
keycode 109 = PageDown  # Was Next.
One very nice feature is to be able to hold down the ALT key while using the arrow keys to pan within Emacs. Since ALT-Left and ALT-Right were previously used to switch virtual consoles, those functions will be remapped to CTRL-Left and CTRL-Right.

Change these two lines as shown:

# Ctrl-Left (was Alt)
control keycode 105 = Decr_Console
# Ctrl-Right (was Alt)
control keycode 106 = Incr_Console

And add the following lines:

alt     keycode 103 = F117   # Left Alt-Up Arrow
altgr   keycode 103 = F117   # Right Alt-Up Arrow
alt     keycode 105 = F120   # Left Alt-Left Arrow
altgr   keycode 105 = F120   # Right Alt-Left Arrow
alt     keycode 106 = F119   # Left Alt-Right Arrow
altgr   keycode 106 = F119   # Right Alt-Right Arrow
alt     keycode 108 = F118   # Left Alt-Down Arrow
altgr   keycode 108 = F118   # Right Alt-Down Arrow
string F117 = "\\033\\033[A"   # Alt-Up Arrow
string F118 = "\\033\\033[B"   # Alt-Down Arrow
string F119 = "\\033\\033[C"   # Alt-Right Arrow
string F120 = "\\033\\033[D"   # Alt-Left Arrow
Note ALT-arrow transmits an ESCAPE followed by the normal escape sequence for the arrow key.

Of course, CTRL-ALT-DELETE will reboot Linux, but what if the user is finished for the day and wants a quick shutdown? To make CTRL-ALT-H-END shut down Linux, add the following lines:

# Numeric keypad End
control alt     keycode  79 = KeyboardSignal
control altgr   keycode  79 = KeyboardSignal
# Editing keypad End
control alt     keycode  107 = KeyboardSignal
control altgr   keycode  107 = KeyboardSignal

Then save the file, and edit /etc/inittab. Add or edit the following lines as shown:

# Action on special keypress (CTRL-ALT-END).
kb::kbrequest:/sbin/shutdown -h now
Save the file, and everything is ready to be tested. Type:
# loadkeys custom.map
and try the new keys. DELETE should act as BACKSPACE did before. Caps Lock has been switched with the left CTRL key. Exit all programs, then try CTRL-ALT-END. After shutdown, use the hardware reset button to reboot.

If the keyboard passed these basic tests, us.map can be replaced with custom.map. Regular users may encounter errors when running loadkeys, because it requires read access to /dev/console. Furthermore, different users using different maps could cause confusion. Therefore, it is suggested the new keymap be permanently installed on the system by root.

Warning: If these installation instructions are not followed correctly, it is possible to place the keyboard in an unusable state, forcing the user to reboot from the installation floppies. Please take any necessary precautions before proceeding.

To install custom.map permanently, edit /etc/init.d/boot, or whichever boot script contains the loadkeys command, and add the following line to the top of the file:

custom_keys=/usr/lib/kbd/keytables/custom.map

Then replace the loadkeys command with:

if [ -f $custom_keys ]  # If custom keys exist,
then                            # then load them.
  loadkeys $custom_keys
else                            # Else use the regular keys.
  loadkeys /usr/lib/kbd/keytables/us.map
fi
This way, if custom.map somehow gets deleted, the keyboard will still work. Run this script to make sure it is correct. If it works, the new keymap will be automatically activated at the next system boot.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Ctrl-m in emacs

softexp23's picture

How can I bind Ctrl-m to some command, in emacs, without binding return to that command. So how can I separate Ctrl-m from return ? And not using X. Cause I use emacs over ssh.

Thanks

Re: Consistent Keyboard Configuration

jago25_98's picture

Nice.

However, what do you do if the loadkeys command isn't present?

Ditto for the find command.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState