• Aucun résultat trouvé

You won’t be able to use a keyboard key if there is no keycode associated with the scancode. Moreover, every application that uses a custom keyboard driver tracks its own keyboard mapping. This means that you will need to map keycodes for both the console and graphical desktop.

Mapping Console Keys

The kernel includes a set of built-in mappings that are standard among keyboard manufacturers. Additional mappings can be created in the boot-time keymap, which is used by the console. The location of the boot-time keymap varies by operating system. For example:

Dapper Drake (6.06 LTS) and Hardy Heron (8.04 LTS) use /etc/

console/boottime.kmap.gz.

Jaunty Jackalope (9.04) uses/etc/console-setup/boottime.kmap.gz. Karmic Koala (9.10) uses/etc/console-setup/cached.kmap.gz.

The compressed keymap (kmap) file contains lines that follow a basic format:

[modifier] keycode number = keysym keysym keysym...

The keycode’s number is effectively the scancode value. It may be in decimal or hex. The key symbols (keysym) define the keycodes that are generated. Usually there is one or two keysym values listed. The first is the basic, unmodified key, and the second is a combination with the shift key.

The keysym value can be a character, like ‘‘a’’ or a common name. The keyboard driver recognizes names like graveandasciitilde, so the actual character is not required in the file.

For example, Hardy Heron’s default keymap file maps scancode 6 to the

‘‘5’’ key, and shift with the scancode is a percent (%). Other key combinations include holding down the modifiers Control, Alt, and Alt+Shift.

keycode 6 = five percent

control keycode 6 = Control_bracketright alt keycode 6 = Meta_five

shift alt keycode 6 = Meta_percent

KEYMAP STRINGS AND COMPOSITION

The keymap file contains other types of mappings. For example, strings can be assigned to a scancode. If a single scancode is seen, it can generate a series of letters. Definingstring F9 = “Hello“will generateHelloevery time you press F9. The default configuration assigns ASCII terminal escape sequences to function keys.

The keymap also includes composition mappings. Using the compose key (found on non-English keyboards), you can select two keys and generate a character. For example, the default setting includescompose 'a’ 'e’ to '<E6>’. Pressing the compose key followed by ‘‘a’’ and ‘‘e’’ will create the ae ligature, æ (character 0xE6).

Of course, using this composition requires a Compose key on your keyboard.

Since the Compose key is usually not found on U.S. keyboards, you will need to define one. For example, on my keyboardkeycode 100is the right Alt key (also called Alt Graph or AltGr). Usingkeycode 100 = Composeallows me to redefine the key to be the Compose key. Then, I can pressCompose “ y (press-ing one key at a time) to generate a ¨y character.

To create your own keyboard mapping, start with the current mapping.

1. Press Ctrl+Alt+F2 and log in to the text console.

2. Uncompress the current boot-time keymap or generate a new one by usingdumpkeys.

cd $HOME

zcat /etc/console/boottime.kmap.gz > boottime.kmap # Hardy or Dapper dumpkeys > boottime.kmap # all versions of Ubuntu

3. Edit the uncompressed keymap file, and insert your own mappings.

4. Recompress the file usinggzip, and replace the old file.

5. Load the new keymap.

sudo loadkeys /etc/console/boottime.kmap.gz # Hardy or Dapper

Your new keymap will be activated when you load it, and it will be loaded with each reboot.

Mapping Desktop Keys

The X-server manages the desktop key mappings. The commandxmodmap -pke displays the list of keycode and keysym mappings. The keysym mappings are:

keycode num = 1 2 3 4. . . where the positions are:

1. The unmodified key.

2. Shift+key.

3. Mode_switch+key. The mode switch key sequence alternates the key-board layout mode.

4. Shift+Mode_switch+key.

NOTE Don’t be surprised ifxmodmap -pkeshows keycodes without keysym values. The keycode comes from your keyboard, and there are some codes that your keyboard cannot generate.

For example, the default U.S. keyboard layout maps keycode 29 to the letter Y:

keycode 29 = y Y

I can assign an alternate mode using:

xmodmap -e “keycode 29 = y Y yen cent“

This maps the keyboard character ‘‘y’’ to the letter ‘‘y’’ and shift+y generates a capital ‘‘Y’’. Pressing the mode-switch key (such as Alt+Alt) enables the alternate mode, where the keyboard character ‘‘y’’ generates a Japanese Yen symbol (¥) and shift+y creates a cent symbol (¢). Pressing the mode switch again returns to the letter mode.

Altering Keycode Assignments

To find a particular keycode, open a command prompt and use the X Event Tester (xev) program. This pops up a small window and displays every X-server event, including mouse movements and key presses, in the ter-minal window. For example, pressing the ‘‘h’’ key displays the key-down event, key-up event, keycode value, and any active keysym values (see Figure 4-1).

Figure 4-1: The xev application, showing keycodes

To modify the active keyboard map, use xmodmap -e expression. The expression assigns a keycode to a set of keysym values. However, this assignment will only remain until you log out.

For a permanent configuration, create $HOME/.xmodmap and list your key-board changes. The changes will only take effect when you log in (not other users), and Gnome will prompt you about using a new.xmodmap.

TIP After creating.xmodmap, test it using the commandxmodmap .xmodmap. Be sure to correct any errors; otherwise, the configuration will not load.

For a systemwide configuration change, consider adding thexmodmap com-mands to/etc/X11/xinit/xinirrcor/etc/X11/Xsession.

SHAS’T NOS FUNNY!

There’s an old practical joke where people pry the keys off a keyboard and replace them out of order. A user who is not a touch typist can get really con-fused when looking for the right letters. Thexmodmapcommand can be used as an excellent alternative to this prank. For example, you can add the follow-ing commands to the victim’s.bashrcfile:

xmodmap -e 'keycode 91 = t T’

xmodmap -e 'keycode 92 = s S’

This prank swaps the s and t keys on the keyboard without requiring any physical keyboard modifications. Alternately, if you can pry up and replace

SHAS’T NOS FUNNY!

the keys on the keyboard, follow it up by remapping the keyboard so that the keycodes match the keysym values! Be sure to wear tennis shoes if you do this, since your coworker will either get a big laugh out of it, or you will need to run.

Running Commands with the Push of a Button

Now that you can identify unused keys and map them to characters, it is time to put them to use. The X-server includes some special-function keysym values. For example,XF86WWWandXF86HomePageare the buttons that should start your web browser. My keyboard includes a special key that looks like a globe; xev identifies it as keycode 180. This button is supposed to start my web browser. To make it fully functional, I need to make the following changes:

1. Usexmodmapto assign the key to the XF86HomePage keysym:

xmodmap -e 'keycode 180 = XF86HomePage’

2. Open SystemPreferencesKeyboard Shortcuts.

3. Scroll down to the ‘‘Launch web browser’’ setting.

4. Click on the shortcut. It will prompt for you to select a new shortcut key.

5. Press the globe key, and the shortcut should say XF86HomePage. Now, every time you press the globe button, it will start up a new web browser.

TIP If you like this hack, then be sure to either addkeycode 180 = XF86HomePageto$HOME/.xmodmap, or add the entirexmodmapline to

/etc/X11/xinit/xinitrc. Otherwise, this change will be lost the next time you log in.

Besides the default events listed in the Keyboard Shortcuts applet, you can define your own. Use the graphical gconf-editor or command-line gconftool-2 to alter your Gnome configuration. There are 12 run_command options under/apps/metacity/global_keybindings. These allow you to bind key combinations to commands. The commands to run when the binding is found are listed under/apps/metacity/keybinding_commands. For example, to map Alt+Shift+T so that it opens a new terminal window, use:

gconftool-2 -t str -s /apps/metacity/global_keybindings/run_command_9 \ '<Alt><Shift>t’

gconftool-2 -t str -s /apps/metacity/keybinding_commands/command_9 \ 'gnome-terminal --geometry 80x40’

Of course, the predefined events in the Keyboard Shortcuts and 12 user-defined sequences may not be enough for some people. In that case, use xbindkeysto associate any arbitrary key sequence with a shell command.

1. Installxbindkeys.

sudo apt-get install xbindkeys

2. Create a configuration file. The program looks for$HOME/.xbindkeysrc. You can create a default configuration using:

xbindkeys --defaults > $HOME/.xbindkeysrc

3. Each mapping consists of two lines. The first is a quoted string con-taining the command to run. The second is indented and lists the key combinations. For example:

“gnome-terminal --geometry 80x40“

alt + shift + t

TIP xbindkeysis not limited by the type of key combination. For example, control + b:1 + altwill run the command when the combination control key, alt key, and mouse button #1 (left mouse button) are pressed at the same time.

4. Run thexbindkeysdaemon. For a permanent configuration, be sure to addxbindkeys &to/etc/X11/Xsession.

OUT OF KEY

With Jaunty Jackalope and Karmic Koala, the audio volume control on my Toshiba laptop causes the popup notification box to go bonkers and freezes the desktop. This happens because the dial generates an infinite stream of key volume events. Adding to this problem, the little dial is far too easy to bump.

If you experience this problem, then consider disabling the volume dial. One way do this is to open the Keyboard Shortcuts and remove the Volume Up and Volume Down shortcuts by pressing the backspace key. Alternately, you can usexmodmapto remove the key assignments.

$ xmodmap -pke | grep -e XF86AudioRaise -e XF86AudioLower

keycode 122 = XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume

keycode 123 = XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume

$ xmodmap -e 'keycode 122 = NoSymbol’

$ xmodmap -e 'keycode 123 = NoSymbol’

If you need volume control, you can always map it to a different key setting.

For example, I installed a command-line audio mixer (sudo apt-get install

OUT OF KEY

aumix). The following code forxbindkeysmaps two key sequences that alter the volume level by 10%.

“aumix -v+10“

Control+Shift+F7

“aumix -v-10“

Control+Shift+F6

Examples of Keyboard Shortcuts

There are many great shortcuts that can be mapped to key combinations. For example, my system contains the following mappings:

The left Super key (usually has a Microsoft logo on PC keyboards or a flower symbol on Macs) is mapped to Audio Mute. When the phone rings, I may not be able to quickly stop every music player, web page with background sounds, and other audio applications. Instead, I press Super_L and immediately mute the entire computer. Pressing it again unmutes the system.

While Macintosh keyboards have a button to eject the CD, most PC keyboards lack this button. My Menu key (found on the bottom right, next to the Control key) ejects the CD-ROM drive.

The key combination Alt+Shift+B immediately starts a systemwide backup. I also installed the notify-send command (sudo apt-get install libnotify-bin). This way, completed backups show a noti-fication popup on the desktop, and the message remains up for 60 seconds.

notify-send -t 60000 “Backup completed“

Control+Alt+b:2 (using xbindkeys, the middle mouse button) runs

“sleep 1; xkill“. This allows me to click on any window and imme-diately kill it. (The sleep statement provides time to release the key sequence. Otherwise,xkillcannot grab the cursor.)

The combination Alt+Shift+G runs my favorite game (bzflag).

Many of the Gnome applications use Ctrl+key and Alt+key combinations. I generally stay away from using these in customized key sequences because of potential conflicts. However, three-key combinations, including Ctrl+Alt+key and Alt+Shift+key, rarely conflict with other applications. Similarly, the Super_L and Super_R keys are usually not used by other applications.

Documents relatifs