Customize your laptop keyboard with X and KDE

592

Author: Federico Kereki

I am a Linux user, and I recently got an eMachines laptop. Since I’m Uruguayan, my mother tongue is Spanish, and that presented a problem: laptops usually have an American-style keyboard, and Spanish (as well as Portuguese, French, German, and other languages) requires some special keys that aren’t on American keyboards. Here’s how you can get international characters on your American keyboard — and as a bonus, we’ll see how you can enable the special “media” or “Internet” keys on some keyboards that aren’t supported by Linux out of the box.

When you set up Linux (I’m using openSUSE 10.2) the keyboard on most laptops is detected as “Generic 104 key PC” (or 102, or a similar number; check your laptop) with a “US English” layout. In such a keyboard, you won’t have any special keys for letters such as “Ñ” or “Ç,” and you also won’t be able to use tildes to get “ã” or “ü.” (And, by the way, the Windows ALT+nnn combinations won’t work, so don’t even bother.) However, you can work around this by “composing” the desired characters by pressing a pair of keys.

If you are feeling adventurous (or you don’t use openSUSE) you can edit the /etc/X11/xorg.conf file directly with any text editor. Look for the “InputDevice” section that deals with the keyboard, change it to look like the section below — note the XkbVariant line — and the next time you start a GUI session, the keyboard will be OK.

Section "InputDevice"
    Identifier     "Keyboard[0]"
    Driver         "kbd"
    Option         "Protocol" "Standard"
    Option         "XkbLayout" "us"
    Option         "XkbModel" "pc104"
    Option         "XkbRules" "xfree86"
    Option         "XkbVariant" "intl"
EndSection

If you prefer an easier way to do this under openSUSE, open YAST, and in the Hardware page, go to the Keyboard Layout option. Specify “INTL” (for “International”) in the Variant field, just below Type and Layout. This will convert some of the keyboard keys to “dead keys,” which means they won’t work on their own; they will have to be combined with other keys. For example, to get an “ñ” you would have to press ˜ first (but nothing would show on screen; hence the “dead key” name) and then the “n” key. Other similar combinations are:

To get… Key in…
Á É Í Ó Ú á é í ó ú     first ´ then the vowel
À È Ì Ò Ù à è ì ò ù     first ` then the vowel
Ã Õ Ñ ã õ ñ     first ˜ then the letter
Ä Ë Ï Ö Ü Ÿ ä ë ï ö ü ÿ     first then the letter

To get a tilde by itself, either press the key twice in a row (thus, ˜ ˜ will produce a single ˜ character) or press the key first, then press the space bar.

The previous table is not complete: French and Portuguese speakers will notice that “ç” is missing, German speakers will miss “ß,” and some English purists will want to be able to key in “æ.” For this, you will have to press the AltGr key (the Alt key on the right of the space bar) and, without releasing it, press another different key, and thus produce the desired special character. Not all combinations are easy to remember, but …

To get … Press AltGr and …
Å å     W or w
ß     s
Æ æ     Z or z
Ç ç     ; or ,
    5
£     $
¥    

Feel free to experiment with other combinations; you can get several other less-used characters this way.

Having solved the problem of making foreign characters available, let’s get to work on the special media keys.

What’s that key?

There are at least a couple of ways to get most special keys to work. The harder one requires defining a “keyboard map” (check out a program called lineak if you are interested). But there’s a simple way, using the xmodmap program.

For each key we want to remap, we first have to identify what the key produces when pressed, and only after that we will be able to make a key perform a specific action. To identify the so-called keycode, open a command window, and key in the xev command. It will bring up a white window with a small rectangle on it, and lots of messages will start appearing on the command window itself, corresponding to every event, such as pressing a key, clicking on something, and so on. Pick any of the special keys, press it, and note what shows up in the command window. For example, when I press the “Internet” key, I get:

KeyRelease event, serial 31, synthetic NO, window 0x2000001,
    root 0x4c, subw 0x0, time 2941185, (78, -10), root:(1031:1032),
    state 0x0, keycode 178 (keysim 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

The important thing here is the number after “keycode,” which is the scan code for the key. In this case, I see that the code for “Internet” key is 178. You will have to go through this process for each key you want to map. (Strange things might happen; for example, I discovered that I had no control over the “Increase Screen Brightness” and “Decrease Screen Brightness” keys. I guess they are hardwired.)

Now you know your keys, but Linux still doesn’t; you have to assign keycodes to them. You can find the needed definitions in /usr/share/X11/XKeySymDB — look near the end of the file for lines starting with “XF86” and decide which ones match your special keys. After doing this for all of my keys, I created a file named .Xmodmap with the following definitions and placed it in my home directory:

keycode 237 = XF86AudioPlay
keycode 164 = XF86AudioStop
keycode 144 = XF86AudioPrev
keycode 153 = XF86AudioNext
keycode 174 = XF86AudioLowerVolume
keycode 176 = XF86AudioRaiseVolume
keycode 178 = XF86HomePage
keycode 229 = XF86Search
keycode 326 = XF86Mail

The format of this file is simple: each line maps a code (the ones we found with xev) to a symbol. This file will be loaded whenever you start a X session by a script (/etc/X11/xinit/xinitrc.common) that gets run automatically and includes a call to xmodmap. If this didn’t work, you would have to cd to your home directory and run xmodmap .Xmodmap on your own.

Making keys do something

Now we are almost done; let’s make the keys actually do something. Open KDE’s Control Center, and in the Regional & Accesibility tab, pick the Input Actions option. Add a group (use the New Group command), and name it something like Laptop Media Keys. Then, for each key:

  • Add a New Action to your group.
  • Go to the General tab, and give your action a name in the Action Name field.
  • Pick the action type: usually, it will be Keyboard Shortcut -> Command/URL (simple).
  • Go to the Keyboard Shortcut tab, click on the button there, and press the special key you want to associate with this action. You can use Shift and Control with the key.
  • Go to the Command/URL Settings tab, and type in the command that should be executed when the key is pressed.

After this process, my “Internet” button invokes /usr/bin/firefox, while my “Find” button runs kfind, and the “Play” button calls amarok; you can make your own choices. After doing this for each of your special media keys, you can apply your changes and start using the keys right away.

Summary

Linux is quite flexible, and you can use it on your laptop no matter what language you speak. Furthermore, you can make all special function keys do whatever you want. After all, why shouldn’t you be able to fully use your own machine?

Categories:

  • Desktop Hardware
  • Desktop Software
  • System Administration
  • Tools & Utilities