Linux – Determining Keycode Assignments for X.org on a PC

keyboardlinuxxmodmapxorg

At some point around late 2008 to early 2009, some keycode values (apparently, for the keys that weren't on an XT keyboard) on a standard PC keyboard when running X.org under Linux changed. I don't know whether this was due to a kernel change or an X change. Debian lenny and Ubuntu 8.04 used the old assignment; Debian squeeze and Ubuntu 10.04 used the new assignment.

So I have two xmodmaprc files, one with the old keycodes and one with the new keycodes. How do I determine which one to load from my .xinitrc and .xsession?

I want my startup files to be portable with no hassle, so I want to get rid of my present dependency on the machine name (!). And I don't want to introduce a dependency on Linux distribution versions (and anyway that would be information about the client, but it's information about the server that I need). I already have information about the server at this point in my script (all the symbols listed in xrdb(1) are available as environment variables); hopefully my question reduces to “What parameters to I compare to what values?”.

Note that since I want my startup files to work on machines that I don't administer, keysym reassignments alone are no help. For example, I want to assign my favorite modifier to the Win keys, but I can't predict what keysym will be assigned when I log in. However, if there is an XKB concept I can use in place of keycodes (and is independent of any sysadmin customization, so keysyms won't do), I'm willing to switch to XKB.

Best Answer

You can find out the keycode of a key through XKB, for example

xkbcomp -a -dflts -xkb $DISPLAY - | sed -ne 's/^ *<LWIN> *= *\([0-9][0-9]*\).*/\1/p'