Linux, KDE: Mapping functions to extra keys

From juliano.info

Jump to: navigation, search
F8-battery.png

My laptop (an HP/Compaq nx6110 notebook) has this extra non-standard special function key that is (normally) only available on Windows, through a special driver installed. Pressing it while running Linux does nothing but generate these messages on syslog:

kernel: atkbd.c: Unknown key pressed (translated set 2, code 0x89 on isa0060/serio0).
kernel: atkbd.c: Use 'setkeycodes e009 <keycode>' to make it known.
kernel: atkbd.c: Unknown key released (translated set 2, code 0x89 on isa0060/serio0).
kernel: atkbd.c: Use 'setkeycodes e009 <keycode>' to make it known.

No events are registered on X. I was bothered and decided to make it useful on Linux too.

Step 1: Assign a keycode for the key on the kernel

The first thing is to make the key known to the kernel, assigning a keycode to it, like the messages instructed. I looked at my keyboard map (located in /lib/kbd/keymaps/i386/qwerty/us-acentos.map.gz) and found that codes 120 to 127 were free. So, I assigned the keycode 120 to this key:

# setkeycodes e009 120

Add this command to your rc scripts (mine is /etc/rc.d/rc.local) to do it every time the system boots.

Step 2: Assign a keycode for the key on X

Now that the key is known to the system, we run xev in X and press the key to check if it is being detected and which events it generates.

$ xev
...
KeyPress event, serial 32, synthetic NO, window 0x3200001,
    root 0x5d, subw 0x0, time 3557653540, (-38,315), root:(554,337),
    state 0x0, keycode 139 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyRelease event, serial 32, synthetic NO, window 0x3200001,
    root 0x5d, subw 0x0, time 3557653631, (-38,315), root:(554,337),
    state 0x0, keycode 139 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

So, in X, the key generates KeyPress/KeyRelease events with keycode 139. Since X still doesn't know what this key should do, it lists it with a null keysym (no symbol assigned). To assign a symbol to this key, we use xmodmap. There are many symbols available for you to choose from, see the files XF86keysym.h and keysymdef.h in /usr/include/X11. I chose XF86LightBulb for my key, it was the closest thing I found that remember a battery, power management, etc. To assign the symbol, add the following line to your /etc/X11/Xmodmap:

keycode 139 = XF86LightBulb

To load the key without having to restart X, run:

$ xmodmap /etc/X11/Xmodmap

Now we run xev and press the key again to check if the key is properly assigned the symbol:

$ xev
...
KeyPress event, serial 32, synthetic NO, window 0x3000001,
    root 0x5d, subw 0x0, time 3561393936, (-234,4), root:(490,366),
    state 0x0, keycode 139 (keysym 0x1008ff35, XF86LightBulb), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyRelease event, serial 32, synthetic NO, window 0x3000001,
    root 0x5d, subw 0x0, time 3561394057, (-234,4), root:(490,366),
    state 0x0, keycode 139 (keysym 0x1008ff35, XF86LightBulb), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

Great, The key is assigned to the symbol XF86LightBulb. Now we proceed to make KDE do something when the key is pressed.

Step 3: Assign an action to the key

F8-kpowersave.png

On Windows, this key opens some status information about the laptop's battery. On KDE, we have kpowersave that have a dialog for the same purpose. In KDE, we can use DCOP to command applications to do things, it is the subsystem used by KDE to do remote procedure calls. We will use it to command kpowersave to open or close the battery dialog.

F8-khotkeys.png

In KDE control center, under "Regional & Accessibility", "Input Actions", we create a new action, with the following settings:

  • General:
    • Action name: Battery status
    • Action type: Keyboard Shortcut -> DCOP Call (simple)
  • Keyboard Shortcut
    • Select keyboard shortcut: XF86LightBulb
  • DCOP Call Settings
    • Remote application: kpowersave
    • Remote object: KPowersaveIface
    • Called function: showDetailedDialog
    • Arguments: (empty)

Confirm and that is all. You should be able to use the key to open and close kpowersave battery status window.

You may also run kdcop to inspect and find other functions in other applications that you can use to command remotely.

Views