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?
Asked
Active
Viewed 5,121 times
1 Answers
6
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
- 8,365
- 1
- 31
- 46
-
1But what if user changes keyboard layout to something non-US? – Mad Fish Aug 25 '10 at 17:36
-
Yes, that's it. I've used XLookupString() by mistake instead of XKeycodeToKeysym(). – Mad Fish Aug 25 '10 at 18:26
-
1If 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 Aug 26 '10 at 04:24