尝试在 Ubuntu Linux 中构建全局键盘挂钩时出错 [英] Error when trying to build a Global Keyboard Hook in Ubuntu Linux

查看:23
本文介绍了尝试在 Ubuntu Linux 中构建全局键盘挂钩时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个代码来在 KeyPress 和 KeyRelease 事件中获取键盘输入,但是当我运行该程序时没有任何反应,我的代码有什么问题?以及如何获取按下的键值?

I'm developing a code to get keyboard input in KeyPress and KeyRelease events, but when i run the program nothing happens, what is wrong with my code? And how to get the pressed key value?

void hook()
{
    forever
    {
        XEvent event;
        Display *dpy = XOpenDisplay(NULL);
        XNextEvent(dpy, &event);
        switch (event.type)
        {
        case KeyPress:
            qDebug() << "keypress";
            break;
        case KeyRelease:
            qDebug() << "keyrelease";
            break;
        }
    }
}

推荐答案

阅读更多关于 键盘 X11 事件.您只能从某些 X11 窗口获取它们,如果该窗口在其 事件掩码.该窗口应该是 created InputOnly输入输出

Read more about keyboard X11 events. You will get them only from some X11 windows, if that window has set some of KeyPressMask or KeyReleaseMask bits in its event mask. And that window should be created InputOnly or InputOutput

您显然在使用 Qt(这是个好主意).然后,坚持 Qt key 事件.

You are apparently using Qt (which is a good idea). Then, stick to Qt key events.

(如果您想捕获所有 X11 按键事件,请使用显示的根窗口.但是您会干扰您的窗口管理器,这是一个坏主意;了解更多关于 ICCCMEWMH)

(if you want to catch all X11 key events, use the root window of the display. But then you are interfering with your window manager, which is a bad idea; learn more about ICCCM and EWMH)

此外,在终端中运行 xev 以了解有关 X11 事件的更多信息

Also, run xev -in a terminal- to understand more about X11 events

这篇关于尝试在 Ubuntu Linux 中构建全局键盘挂钩时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆