从键码中获取键名 (X11 XGrabKey) [英] Getting key name from keycode (X11 XGrabKey)

查看:20
本文介绍了从键码中获取键名 (X11 XGrabKey)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 linux 中有一个全局键事件处理程序,如下所示.我需要知道哪个键盘被抓住了.例如,如果按下P"键,我会得到相应的键码.有没有办法从这个未签名的密钥代码中获取密钥名称(P")?

I have a global key event handler in linux as below. I need to know which keyboard is grabbed. For example if key 'P' is pressed I get the corresponding key code. Is there any way to get the key name ("P") from this unsigned key code ?

#include <xcb/xcb.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <QtX11Extras/QX11Info>

void EventFilter::setup(QWidget *target)
{
    this->target = target;

    Display * display = QX11Info::display();
    unsigned int modifiers = ControlMask;
    keycode = XKeysymToKeycode(display, XK_A);
    XGrabKey(display, keycode, modifiers, DefaultRootWindow(display), 1, GrabModeAsync, GrabModeAsync);
}

bool EventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
    if (eventType == "xcb_generic_event_t")
    {
        xcb_generic_event_t* xcbevent = static_cast<xcb_generic_event_t *>(message);

        switch(xcbevent->response_type)
        {
        case XCB_KEY_PRESS:
            xcb_key_press_event_t * keypress_event = static_cast<xcb_key_press_event_t *>(message);
            if(keypress_event->state & XCB_MOD_MASK_CONTROL)
            {
                if(keypress_event->detail == keycode)
                {
                    //print key name here
                }
            }
        }
    }
   return false;
 } 

推荐答案

给定一个关键代码,从事件 detail 字段中,您可以使用 KeySym 获取KeySymcode>XkbKeycodeToKeysym 函数,然后是按下键的文本表示,将 KeySym 传递给 XKeysymToString 函数.

Given a key code, from the event detail field, you can get the KeySym using the XkbKeycodeToKeysym function, then a textual representation of the pressed key, passing the KeySym to the XKeysymToString function.

有这个额外的包括:

#include <X11/XKBlib.h>

然后,在事件处理程序中:

Then, in the event handler:

case XCB_KEY_PRESS:
    xcb_key_press_event_t * keypress_event = static_cast<xcb_key_press_event_t *>(message);           
    xcb_keycode_t code = keypress_event->detail;
    qDebug() << XKeysymToString( XkbKeycodeToKeysym(QX11Info::display(), code, 0, 0) );

在上面的示例中,0 的索引作为 XkbKeycodeToKeysym 的最后一个参数传递.这将返回按下的键的符号,就好像没有按下 shift 键(或大写锁定,或任何其他修饰键)一样.传递 1 的索引将返回符号,就像按下了 shift 键一样.其他值(即 2)将产生一个按更多修饰键获得的符号(例如,在我的意大利语键盘中,我必须按 Alt Gr 来输入方括号).

In the example above, an index of 0 is passed as the last argument of XkbKeycodeToKeysym. This will return the symbol for the pressed key as if the shift key (or caps lock, or any other modifier key) is not pressed. Passing an index of 1 will return the symbol as if the shift key was pressed. Other values (i.e. 2) will yield symbols one obtains pressing more modifier keys (e.g. in my italian keyboard I have to press Alt Gr to type square brackets).

请注意,返回的字符串实际上是一个用于标识键盘符号的名称,例如可以是abcX 用于字母,但 comma反斜杠 用于其他符号.

Please notice, the returned string is really a name to identify the keyboard symbol, which can be, for example, a, b, c or X for letters, but comma, or backslash for other symbols.

这篇关于从键码中获取键名 (X11 XGrabKey)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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