2
votes

I need to know how to inject a KeySym into the X Server using xcb, xtest or xlib. I can inject keycodes, also get the keyboard mapping, search for a keysym there and press the according keycode, but i cannot inject a KeySym (e.g. XKB_KEY_Arabic_0) when its not mapped to the current layout.

Is there a way I can do that?

What I thought of is find an empty keycode and remap it to the desired keysym. Problem: My code doesnt find an empty key. This is my code:

uint32_t find_empty_key_for_remapping()
{
    int keycode_low;
    int keycode_high;
    KeySym * keysyms = 0;
    int empty_key = 0;
    int num_keysym;
    XDisplayKeycodes(display_, &keycode_low, &keycode_high);
    keysyms = XGetKeyboardMapping(display_, keycode_low, keycode_high - keycode_low, &num_keysym);
    for (int i = keycode_low; i <= keycode_high && !empty_key; ++i)
    {
        bool key_is_empty = true;
        for (int j = 0; j < num_keysym; ++j)
        {
            int symindex = ((i - keycode_low) * num_keysym) + j;
            if (keysyms[symindex] != 0)
            {
                key_is_empty = false;
            }
        }
        if (key_is_empty)
        {
            return i;
        }
    }
    return 0;
}

Any help will be appreciated!

1
You can use XChangeKeyboardMapping and add extra keycodes. You would need to do that each time the mapping changes. - n. 1.8e9-where's-my-share m.
Can I really add new keycodes? I thought you can just remap existing ones? - Nidhoegger
Hmm, I don't see why not. Need to experiment. - n. 1.8e9-where's-my-share m.
As the keycode is an uint8_t and there exist 256 (0 - 255) of them, I dont think that creating keycode 256 will be successful. First I tried to seek for an unset keycode, but I dont unerstand the mapping in X when multiple keyboard layouts are set (then one keycode has up to 15 fields, all reachable via layout + modifier) - Nidhoegger
Sorry my mistake, you cannot add keycodes. - n. 1.8e9-where's-my-share m.

1 Answers

-1
votes

Injecting keysyms is done with xdotool. To insert XKB_KEY_Arabic_0 use

xdotool key Arabic_0

which is independent of the keyboard mapping.