Getting key name from keycode (X11 XGrabKey)

孤人 提交于 2019-12-25 01:46:04

问题


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;
 } 

回答1:


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.

Have this extra include:

#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) );

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).

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.



来源:https://stackoverflow.com/questions/49491918/getting-key-name-from-keycode-x11-xgrabkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!