如何在 Linux 上捕获键盘布局更改事件并获取当前的新键盘布局?

zha*_*123 8 linux keyboard layout

现在,我正在开发 Linux 上的应用程序,并希望捕获键盘布局更改事件(通过 UI/widget/shell/programing 等更改键盘布局)并获取/设置新的键盘布局信息以进一步处理。我认为这不是一个新问题,但我一次又一次地从 stackoverflow 搜索,但没有答案。希望我能在这里得到正确的答案!

我想学习的主要解决方案在这里描述。在 Windows 中,WM_INPUTLANGCHANGE 窗口消息可以在 WinProc 中捕获,其中包含键盘布局信息。我们可以使用 GetKeyboardLayout() API 来获取当前的键盘布局信息。最后,如果我想使用我喜欢的键盘布局,我可以使用 ActivateKeyboardLayout() 来激活键盘布局。

综上所述,我希望在 Linux 中找到通知消息以及如何在代码中捕获消息(最好给我举个例子),Linux 中的获取键盘布局 API 和设置键盘布局 API。开发语言也是C/C++。

提前致谢。

gvl*_*sov 7

另一个答案对我不起作用。它编译并运行,MappingNotify但当我切换布局时不会发生事件。这是对我有用的修改。

#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>

int main(int argc, char **argv)
{
        XEvent e;
        Display *d;

        if (!(d = XOpenDisplay(NULL))) {
                fprintf(stderr, "cannot open display\n");
                return 1;
        }

        XKeysymToKeycode(d, XK_F1);

    int xkbEventType;
    XkbQueryExtension(d, 0, &xkbEventType, 0, 0, 0);
    XkbSelectEvents(d, XkbUseCoreKbd, XkbAllEventsMask, XkbAllEventsMask);

    XSync(d, False);

    while (1) {
        XNextEvent(d, &e);
        if (e.type == xkbEventType) {
            XkbEvent* xkbEvent = (XkbEvent*) &e;
            if (xkbEvent->any.xkb_type == XkbStateNotify) {
                int lang = xkbEvent->state.group;
                if (lang == 1) {
                    fprintf(stdout, "1\n");
                    fflush(stdout);
                } else {
                    fprintf(stdout, "0\n");
                    fflush(stdout);
                }
            }
        }
    }

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

编译:

gcc -Wall -O2 xmappingnotify.c -o xmappingnotify -lX11
Run Code Online (Sandbox Code Playgroud)

我像这样使用它:

xmappingnotify | xargs -I {} my-custom-command {} &
Run Code Online (Sandbox Code Playgroud)

所以my-custom-command [number_of_the_layout]当我改变布局时运行。我有 2 个布局,xmappingnotify一个布局输出“1”,另一个布局输出“0”。


gol*_*lum 3

X11的答案:

通过MappingNotify事件检测更改

通过setxkbmap更改或查询布局

下面是一个用于检测 MappingNotify 事件的基本 xlib 示例:

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

int main(int argc, char **argv)
{
        XEvent event;
        Display *dpy;

        if (!(dpy = XOpenDisplay(NULL))) {
                fprintf(stderr, "cannot open display\n");
                return 1;
        }

        /**
         * Note: We might never get a MappingNotify event if the
         * modifier and keymap information was never cached in Xlib.
         * The next line makes sure that this happens initially.
         */
        XKeysymToKeycode(dpy, XK_F1);

        while (1)
        {
                XNextEvent(dpy, &event);
                if (event.type == MappingNotify) {
                        XMappingEvent *e = (XMappingEvent *) &event;
                        if (e->request == MappingKeyboard) {
                                fprintf(stderr, "The keyboard mapping was changed!\n");
                        }
                        XRefreshKeyboardMapping(e);
                }
        }

        return(0);
}
Run Code Online (Sandbox Code Playgroud)

内置命令:

gcc -Wall -O2 xmappingnotify.c -o xmappingnotify -lX11
Run Code Online (Sandbox Code Playgroud)