Replace of XKeycodeToKeysym
11,039
Solution 1
Provided XKB
is available then the simplest replacement for XKeycodeToKeysym
is:
#include <X11/XKBlib.h>
/* which declares:
KeySym XkbKeycodeToKeysym(Display *dpy, KeyCode kc,
unsigned int group, unsigned int level); */
... and then the original question's code could become:
mykey = XkbKeycodeToKeysym( display, event.xkey.keycode,
0, event.xkey.state & ShiftMask ? 1 : 0);
notes:
- this chromium bug report discusses both the
XGetKeyboardMapping
and theXKB
replacement solutions. - XkbKeyCodeToKeysym man page on X.org
Solution 2
Show you an example and you can do the same on your source.
Replace
keysym = XKeycodeToKeysym(dpy,xe->xkey.keycode,0)
with
{
int keysyms_per_keycode_return;
KeySym *keysym = XGetKeyboardMapping(dpy,
xe->xkey.keycode,
1,
&keysyms_per_keycode_return);
/* do something with keysym[0] */
XFree(keysym);
}
Not forgetting to free the return value.
Author by
iattila
Updated on June 30, 2022Comments
-
iattila almost 2 years
When I try to build my code with the X11 headers in Ubuntu 12.04
case KeyPress: xcommon_update_server_time( event.xkey.time ); /* if( event.xkey.state & ShiftMask ) arg |= I_SHIFT; */ /* this alternate approach allows handling of keys like '<' and '>' -- mrallen */ if( event.xkey.state & ShiftMask ) { mykey = XKeycodeToKeysym( display, event.xkey.keycode, 1 ); } else { mykey = XKeycodeToKeysym( display, event.xkey.keycode, 0 ); }
What is the expected result? Compiles.
What happens instead?
warning: 'XKeycodeToKeysym' is deprecated (declared at /usr/include/X11/Xlib.h:1695) [-Wdeprecated-declarations]
As a result of https://bugs.freedesktop.org/show_bug.cgi?id=5349 XKeycodeToKeysym is now properly marked as being deprecated.
How to fix my code for warning free and correct build?
Thanks
-
AlwaysLearning over 8 yearsThis should be the accepted answer. It takes care of ShiftMask as well, which saves a lot of headache. Only why does it say "provided XKB is available"? Does it happen that X11 comes without XKB?
-
Noitidart over 7 yearsI am also curious @AlwaysLearning , is it possible for XKB to not be there ? In all the X11 systems I tested it was there.
-
Brent Baccala over 3 yearsXKB isn't there in the Ubuntu 16 tightvnc server.