XKeysymToKeycode和键盘布局

时间:2019-02-01 16:18:55

标签: x11

我正在尝试使用功能XKeysymToKeycode将击键发送到活动的x11应用程序。 该功能有效,但使用的是英文键盘。 我的电脑本地化为意大利语。现在,我必须发送例如意大利语键盘中定义的字符“à”。 我该怎么做才能发送?

1 个答案:

答案 0 :(得分:0)

我一直在努力解决同一问题。似乎是:

  1. 多重布局功能是按组实现的,对于与美国键盘不同的字符,您的IT布局部分在第1组中,对于某些不变键(例如,返回/输入),您的IT布局在第0组中。
  2. XKeysymToKeycode不在乎组,它只选择第一个匹配项。

您可以手动as the clutter guys did完成工作。或结合使用gdk和X:

//char *oksym ="KP_Enter";
char *oksym ="at";
gboolean orv;
guint okeyval;
GdkKeymapKey *okeys;
gint on_keys;
GdkKeymap *okeymap;
GdkDisplay *odisplay;

XkbGetState(display, XkbUseCoreKbd, &xkbState);
printf("Active group would be %d\n",xkbState.group);

odisplay = gdk_display_get_default();
okeymap = gdk_keymap_get_for_display(odisplay);
okeyval = gdk_keyval_from_name(oksym);
if(okeyval == GDK_KEY_VoidSymbol){
    printf("GDK keyval GDK_KEY_%s is unknown\n",oksym);
    exit(-1);
}
orv = gdk_keymap_get_entries_for_keyval(okeymap, okeyval, &okeys, &on_keys);
printf("The following hardware keys will produce keyval GDK_KEY_%s(%d):\n",oksym,okeyval);
for(int i = 0; i < on_keys; i++){
    /*Groups seems to be used to manage multiple keyboard layouts (at least using MATE) 
     * instead/on top of their orignal meaning. Getting the active layout seems to be 
     * acheivable using XkbGetState. Some keys seems to be shared/stay on group 0, example 
     * Return/Enter. So if there is more than one key bound to a keyval, we'll take the one
     * that matches the active group. If there is only one (i.e Enter/Return) we'll just take
     * it regardless of its group.
     * */
    if( okeys[i].group == xkbState.group || on_keys == 1)
        printf("\tkeycode: %d, group: %d, level: %d\n",okeys[i].keycode,okeys[i].group,okeys[i].level);
}