This blog has moved! Redirecting...
You should be automatically redirected. If not, visit http://scrolls.mafgani.net/ and update your bookmarks.

Thursday, August 25, 2005

Custom Keybinding (GNOME)

This is useful for multimedia keyboards that aren't automatically recognized. Also useful when you want actions other that those offered in "gnome-keybinding-properties". The first step is to check if the buttons are recognized at all. This can be accomplised by running "xev"

$ xev


Once the little window pops up, press any of the buttons and see if you get an output like the following:


---------------------------------------------------------------------------------------------
KeyPress event, serial 29, synthetic NO, window 0x3c00001,
root 0xd6, subw 0x0, time 2073408, (1033,644), root:(1051,745),
state 0x0, keycode 231 (keysym 0x0, NoSymbol), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False

KeyRelease event, serial 29, synthetic NO, window 0x3c00001,
root 0xd6, subw 0x0, time 2073529, (1033,644), root:(1051,745),
state 0x0, keycode 231 (keysym 0x0, NoSymbol), same_screen YES,
XLookupString gives 0 bytes:
---------------------------------------------------------------------------------------------


If you do get the output, the important thing to note is the keycode (keycode 231 here). Once you have the keycode for a key, you need to define a Key Symbol for it. To do this, you need a xmodmap file. You can get a copy of it using:


---------------------------------------------------------------------------------------------
$ xmodmap -pke > ~/.xmodmaprc
---------------------------------------------------------------------------------------------


Once the current map is dumped, open the file for editing and locate the required keycode:


---------------------------------------------------------------------------------------------
$ vi ~/.xmodmaprc
[snip]
keycode 231 =
[snip]
---------------------------------------------------------------------------------------------


As we can see there is no symbol associated with the keycode 231 yet. To get a list of available symbols, look at /usr/X11R6/lib/X11/XKeysymDB. Since the button is marked as Refresh, we can use XF86Refresh as a suitable symbol:


---------------------------------------------------------------------------------------------
[snip]
keycode 231 = XF86Refresh
[snip]
---------------------------------------------------------------------------------------------


Once all the required keys have been set, save the file.

To make the new map take effect (in GNOME):


---------------------------------------------------------------------------------------------
$ gconftool-2 --set "/desktop/gnome/peripherals/keyboard/general/known_file_list" --type list --list-type string "[.xmodmaprc]"

$ gconftool-2 --set "/desktop/gnome/peripherals/keyboard/general/update_handlers" --type list --list-type string "[.xmodmaprc]"
---------------------------------------------------------------------------------------------


At this point you probably need to logout and log back in.

Now, for the case where xev doesn't already detect the key. Start by tailing the /var/log/messeges file:


---------------------------------------------------------------------------------------------
$ sudo tail -f /var/log/messages
---------------------------------------------------------------------------------------------


The last lines of the file will be displayed. Now, press the desired key. Now, you should see something like


---------------------------------------------------------------------------------------------
Aug 27 13:55:58 darkworld kernel: atkbd.c: Unknown key pressed (translated set 2, code 0x96 on isa0060/serio0).
Aug 27 13:55:58 darkworld kernel: atkbd.c: Use 'setkeycodes e016 <keycode>' to make it known.
Aug 27 13:55:58 darkworld kernel: atkbd.c: Unknown key released (translated set 2, code 0x96 on isa0060/serio0).
Aug 27 13:55:58 darkworld kernel: atkbd.c: Use 'setkeycodes e016 <keycode>' to make it known.
---------------------------------------------------------------------------------------------


So, as suggested, we make it known by:


---------------------------------------------------------------------------------------------
$ setkeycodes e016 187
---------------------------------------------------------------------------------------------


I chose 187 since it doesn't appear to be used by any key yet. Once all the keycodes have been set, repeat as before to assign the key symbols.

[Edit (Sept. 8, 2005)]
It seems that there is a better way to choose the kernel keycode. We can run getkeycodes to see which keycodes are currently in use. We can use any keycode between 84 and 255 that is not already used.


---------------------------------------------------------------------------------------------
$ getkeycodes
Plain scancodes xx (hex) versus keycodes (dec)
for 1-83 (0x01-0x53) scancode equals keycode

0x50: 80 81 82 83 99 0 86 87
0x58: 88 117 0 0 95 183 184 185
0x60: 0 0 0 0 0 0 0 0
0x68: 0 0 0 0 0 0 0 0
0x70: 93 0 0 89 0 0 85 91
0x78: 90 92 0 94 0 124 121 0

Escaped scancodes e0 xx (hex)

e0 00: 0 195 196 197 198 0 0 0
e0 08: 0 0 0 0 0 0 0 0
e0 10: 165 0 0 0 0 0 0 0
e0 18: 0 163 0 0 96 97 0 0
e0 20: 113 140 164 0 166 0 0 0
e0 28: 0 0 255 0 0 0 114 0
e0 30: 115 0 150 0 0 98 255 99
e0 38: 100 0 0 0 0 0 0 0
e0 40: 0 0 0 0 0 119 119 102
e0 48: 103 104 0 105 112 106 118 107
e0 50: 108 109 110 111 0 0 0 0
e0 58: 0 0 0 125 126 127 116 142
e0 60: 0 0 0 143 0 217 156 173
e0 68: 128 159 158 157 155 226 0 112
e0 70: 0 0 0 0 0 0 0 0
e0 78: 0 0 0 0 0 0 0 0

---------------------------------------------------------------------------------------------


It also seems we need to restore the kernel keycode mapping after every reboot. This is easily accomplished by having the required commands in /etc/rc.local:


---------------------------------------------------------------------------------------------
[darkknight@darkworld ~]$ cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

setkeycodes e016 187 e015 188 e014 189 e013 190 e005 191
e01e 192 e009 193 e00b 202 e008 194 e018 199
e00a 200 e017 201 e03b 203 e03c 204 e03d 205
e03e 206 e03f 207 e040 208 e041 211 e042 210
e043 212 e023 213 e057 214 e058 215 e064 216
[darkknight@darkworld ~]$
---------------------------------------------------------------------------------------------



The kernel keycodes are different from the keycodes used by X. So, we need to run xev again to get the keycodes (as seen by X) of the newly found keys.

Got the hints from here.
[End Edit]

Now that we have KeySymbols, what do we do with them? Open up gconf-editor and browse to the key /apps/metacity/global_keybindings. There, choose any of the run_command_* keys marked as disabled and change the value to the desired KeySymbol (e.g. XF86AudioPrev). Then navigate to the key /apps/metacity/keybinding_commands and find the command_* key with the same number as the run_command_*. Change the value of the key to whatever command you wish to be performed when the Previous key is pressed. If you happen to run out of run_command_* and command_* key pairs, simply create new pairs from the right click menu.

1 Comments:

Anonymous Anonymous said...

That helped a lot!

Please consider, that if You are not using metacity, the last paragrph should be replaced by what is appropriate for Your Window Manager.

I mean: Binding a keysymbol to an action/command.

In Beryl it is (eg)
Start Beryl Manager (somehow ;-).
General Options --> Commands

There You can bind "Keysymbols" easily, after following the steps of DarkKnight.

8:11 PM  

Post a Comment

<< Home