Xkb:如何将键码转换为键符 [英] Xkb: How to convert a keycode to keysym

查看:12
本文介绍了Xkb:如何将键码转换为键符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想获取一个 KeyCode 和一个修饰符掩码,并使用 Xkb 扩展名将其转换为 KeySym.我似乎无法弄清楚为什么这不起作用.很明显修饰符不匹配,但我不知道为什么.我什至不知道我是否正确转换了组.

I am simply trying to take a KeyCode and a modifier mask and convert it to a KeySym using the Xkb extension. I cant seem to figure out why this doesn't work. Its obvious that the modifiers dont match but I dont know why. I don't even know if I am converting the group correctly.

#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/XKBlib.h>

void check(XkbDescPtr keyboard_map, KeyCode keycode, unsigned int mask) {
    //What the hell is diff between XkbKeyGroupInfo and XkbKeyNumGroups?
    unsigned char info = XkbKeyGroupInfo(keyboard_map, keycode);
    int num_groups = XkbKeyNumGroups(keyboard_map, keycode);

    int key_width = XkbKeyGroupsWidth(keyboard_map, keycode);
    //int num_syms = XkbKeyNumSyms(keyboard_map, keycode);

    //Get the group
    unsigned int group = 0; // What should this default to?
    switch (XkbOutOfRangeGroupAction(info)) {
        case XkbRedirectIntoRange:
            /* If the RedirectIntoRange flag is set, the four least significant 
            * bits of the groups wrap control specify the index of a group to 
            * which all illegal groups correspond. If the specified group is 
            * also out of range, all illegal groups map to Group1.
            */
            printf("XkbRedirectIntoRange
");
            group = XkbOutOfRangeGroupInfo(info);
            if (group >= num_groups) {
                group = 0;
            }
        break;

        case XkbClampIntoRange:
            /* If the ClampIntoRange flag is set, out-of-range groups correspond 
            * to the nearest legal group. Effective groups larger than the 
            * highest supported group are mapped to the highest supported group; 
            * effective groups less than Group1 are mapped to Group1 . For 
            * example, a key with two groups of symbols uses Group2 type and 
            * symbols if the global effective group is either Group3 or Group4.
            */
            printf("XkbClampIntoRange
");
            group = num_groups - 1;
        break;

        case XkbWrapIntoRange:
            /* If neither flag is set, group is wrapped into range using integer 
            * modulus. For example, a key with two groups of symbols for which 
            * groups wrap uses Group1 symbols if the global effective group is 
            * Group3 or Group2 symbols if the global effective group is Group4.
            */
            printf("XkbWrapIntoRange
");
        default:
            printf("Default
");
            if (num_groups != 0) {
                group %= num_groups;
            }
        break;
    }
    printf("Group Info %d, %d, %d
", group, num_groups, key_width);
    //printf("Mask Info %d, %d, %d, %d, %d, %d, %d, %d
", ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask);

    XkbKeyTypePtr key_type = XkbKeyKeyType(keyboard_map, keycode, group);

    KeySym keysym = NoSymbol;
    int i;
    for (i = 0; i < key_type->map_count; i++) {
        if (key_type->map[i].active &&  key_type->map[i].mods.mask == mask) {
            keysym = XkbKeySymEntry(keyboard_map, keycode, i, group);
        }
    }

    //printf("%s
", XKeysymToString(keysym));
    printf("KeyCode: %d
", (int) keycode);
    printf("KeySym:  %d
", (int) keysym);
}

int main(int argc, const char * argv[]) {
    Display * display;

    //Try to attach to the default X11 display.
    display = XOpenDisplay(NULL);
    if(display == NULL) {
        printf("Error: Could not open display!
");
        return EXIT_FAILURE;
    }

    //Get the map
    XkbDescPtr keyboard_map = XkbGetMap(display, XkbAllClientInfoMask, XkbUseCoreKbd);

    KeyCode keycode = 56; // b
    check(keyboard_map, keycode, ShiftMask | LockMask | ControlMask);

    //Close the connection to the selected X11 display.
    XCloseDisplay(display);

    return EXIT_SUCCESS;
}

推荐答案

经过多次反复试验,我终于能够弄清楚.XKeycodeToKeysym 显然被破坏了,并且没有为扩展索引定义索引值计算.

I was finally able to figure it out after a lot of trial and error. XKeycodeToKeysym is apparently broken and the index value calculations are not defined for extended indexes.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/XKBlib.h>

KeySym KeyCodeToKeySym(Display * display, KeyCode keycode, unsigned int event_mask) {
    KeySym keysym = NoSymbol;

    //Get the map
    XkbDescPtr keyboard_map = XkbGetMap(display, XkbAllClientInfoMask, XkbUseCoreKbd);
    if (keyboard_map) {
        //What is diff between XkbKeyGroupInfo and XkbKeyNumGroups?
        unsigned char info = XkbKeyGroupInfo(keyboard_map, keycode);
        unsigned int num_groups = XkbKeyNumGroups(keyboard_map, keycode);

        //Get the group
        unsigned int group = 0x00;
        switch (XkbOutOfRangeGroupAction(info)) {
            case XkbRedirectIntoRange:
                /* If the RedirectIntoRange flag is set, the four least significant
                 * bits of the groups wrap control specify the index of a group to
                 * which all illegal groups correspond. If the specified group is
                 * also out of range, all illegal groups map to Group1.
                 */
                group = XkbOutOfRangeGroupInfo(info);
                if (group >= num_groups) {
                    group = 0;
                }
            break;

            case XkbClampIntoRange:
                /* If the ClampIntoRange flag is set, out-of-range groups correspond
                 * to the nearest legal group. Effective groups larger than the
                 * highest supported group are mapped to the highest supported group;
                 * effective groups less than Group1 are mapped to Group1 . For
                 * example, a key with two groups of symbols uses Group2 type and
                 * symbols if the global effective group is either Group3 or Group4.
                 */
                group = num_groups - 1;
            break;

            case XkbWrapIntoRange:
                /* If neither flag is set, group is wrapped into range using integer
                 * modulus. For example, a key with two groups of symbols for which
                 * groups wrap uses Group1 symbols if the global effective group is
                 * Group3 or Group2 symbols if the global effective group is Group4.
                 */
            default:
                if (num_groups != 0) {
                    group %= num_groups;
                }
            break;
        }

        XkbKeyTypePtr key_type = XkbKeyKeyType(keyboard_map, keycode, group);
        unsigned int active_mods = event_mask & key_type->mods.mask;

        int i, level = 0;
        for (i = 0; i < key_type->map_count; i++) {
            if (key_type->map[i].active && key_type->map[i].mods.mask == active_mods) {
                level = key_type->map[i].level;
            }
        }

        keysym = XkbKeySymEntry(keyboard_map, keycode, level, group);
        XkbFreeClientMap(keyboard_map, XkbAllClientInfoMask, true);
    }

    return keysym;
}

int main(int argc, const char * argv[]) {
    Display * display;

    //Try to attach to the default X11 display.
    display = XOpenDisplay(NULL);
    if(display == NULL) {
        printf("Error: Could not open display!
");
        return EXIT_FAILURE;
    }

    KeyCode keycode = 56; // b
    unsigned int event_mask = ShiftMask | LockMask;
    KeySym keysym = KeyCodeToKeySym(display, keycode, event_mask);

    printf("KeySym: %s
", XKeysymToString(keysym));

    //Close the connection to the selected X11 display.
    XCloseDisplay(display);

    return EXIT_SUCCESS;
}

这篇关于Xkb:如何将键码转换为键符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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