xmodmap is dead

October 28, 2014


I’m using an Apple keyboard at work since a day or two, but for some reason this keyboard has a special key for eject, because in 1898 that was useful when you wanted to eject a cdrom.

Apple Keyboard

Anyway making this key do something useful proved incredible painful, especially since xmodmap is deprecated and we should all use xkb (setxkbmap andxkbcomp). In our new world of hotplugging everything this kinda makes sense (xmodmap settings are lost when your USB keyboard goes to sleep).

I use i3, but with gnome-settings-daemon running. So first I removed the eject functionality in there (not tested if this still is needed, with the later fix). In dconf-editor, go to org.gnome.settings-daemon.plugins.media-keys and remove the Value from the key named eject.

Searching more I found this site, which gave me to solution I settled on. With xev I found out the keycode sent is 169.

Dump the current config with xkbcomp $DISPLAY, this creates a file that will be called server-0.xkb

Find the line where keycode 169 is mapped:

key <I167> {         [     XF86Forward ] };
key <I169> {         [       XF86Eject ] };
key <I170> {         [       XF86Eject,       XF86Eject ] };

And change the key <I169> on to use BackSpace (for instance). Compile it to an .xkm file and load the keymap:

xkbcomp server_0.xkb
xkbcomp server_0.xkm $DISPLAY

In xmodmap this would have something like (untested) keycode 169 = BackSpace, which is of course so simple it needed a better solution.

Linux  Xmodmap  Xkb