Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which event is fired when switching kb layout in X.org

Tags:

c++

c

xorg

I'm new to the X.org programming. I want to build up a small application which reacts on the X keyboard layout switch. I've searched, but didn't find which event is fired when the kb layout is switched. Please, point me to the correct event. Thanks

like image 743
Pat Avatar asked Mar 19 '13 12:03

Pat


1 Answers

There's the XkbStateNotify event type, which is part of the X Keyboard Extension. You can grab layout language from it like this:

void x11Events(XEvent* evt)
{
    if(evt->type == xkbEventType) {
        XkbEvent* xkbevt = (XkbEvent*)evt;
        if (xkbevt->any.xkb_type == XkbStateNotify) {
            int lang = xkbevt->state.group;
            // Some code using lang here.
        }
    }
}

To get xkbEventType, call the XkbQueryExtension() function (declared in XKBlib.h).

However, XkbStateNotify is fired not only on layout change. This is from specification referenced above:

The changes reported include changes to any aspect of the keyboard state: when a modifier is set or unset, when the current group changes, or when a pointer button is pressed or released.

Because of this, you'll have to save the value of lang somewhere, and then, when new event arrives, compare new value of lang to the one previously saved.

NB. There's also the XkbMapNotifyEvent event, which notifies not about switching layout per se, but about changing keyboard mapping. You might want to look into that one, too.

like image 161
shakurov Avatar answered Oct 02 '22 22:10

shakurov