Skip to content
Advertisement

Replace of XKeycodeToKeysym

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

Advertisement

Answer

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:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement