9

我是 X.org 编程的新手。我想建立一个对 X 键盘布局开关做出反应的小应用程序。我已经搜索过,但没有找到切换 kb 布局时触发的事件。请指出正确的事件。谢谢

4

1 回答 1

11

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.

于 2013-04-13T10:18:29.130 回答