views:

20

answers:

1

I want to get mapping-independent key codes, but documentation says that "keycode" in XKeyEvent structure depends on hardware and driver and I can't rely on it. How can I get some portable key codes like VK_* in Windows?

+3  A: 

You want key syms, not key codes. See XKeycodeToKeysym() and /usr/include/X11/keysymdef.h

To be strictly correct (especially with internationalization) you need a whole bunch of code along the lines of http://git.gnome.org/browse/gtk+/tree/gdk/x11/gdkkeys-x11.c However, if you're using raw Xlib instead of a toolkit you probably don't care about this kind of thing (if you do you're in for years of work), and XKeycodeToKeysym() is good enough for US keyboards.

Havoc P
But what if user changes keyboard layout to something non-US?
Mad Fish
Yes, that's it. I've used XLookupString() by mistake instead of XKeycodeToKeysym().
Mad Fish
If you care about international keyboards and languages, using a toolkit instead of Xlib is the only sane thing, really. The gtk file I linked to gives you the keyboard part, but leaving out input methods, the equivalent of GtkAccelGroup (making accelerators work with international keyboards), displaying/rendering arbitrary unicode, bidirectional text editing, etc.I'm not sure how to summarize gdkkeys-x11.c briefly... I mean, it isn't easy. I implemented the first version of that file but I don't remember a lot of it.Just XKeycodeToKeysym() is probably good enough for many purposes.
Havoc P