Marco Massagrande
Marco Massagrande

Reputation: 1

X11/Xlib c/c++ char to XKeyEvent keycode

My need is to convert a char into a keycode to send an event with XSendEvent.

I'm using XStringToKeysym("a"), but when I use chars like : I get an invalid result.

Is it possible to bypass use of keysym and convert char directly to XKeyEvent keycode?

Any help appreciated!

Upvotes: 0

Views: 4366

Answers (3)

BobDoolittle
BobDoolittle

Reputation: 1740

Actually, you can do better than @Artyom's answer. If you look at <X11/keysymdef.h> you find that for ASCII 0x20-0xFF, the characters map directly to XKeySyms. So, I'd say it's simpler to just use that range of characters directly as KeyCodes, and just map the remaining 32 characters.

For a more detailed answer including example code, see: https://stackoverflow.com/a/25771958/3149905

Upvotes: 1

Emily
Emily

Reputation: 2684

You can get the Unicode representation of the character and prepend U to it. For instance, in case of : these would do the same thing:

XStringToKeysym("colon")
XStringToKeysym("U003A")

Upvotes: 3

parkydr
parkydr

Reputation: 7784

XStringToKeysym converts a name to a keysym, so ":" won't work, you have to use "colon" instead.

Keycodes are not standard (keyboard layouts vary), hence keysyms are used and are defined in <X11/keysymdef.h>.

The best you can do, is to use XKeysymToKeycode with XK_a and XK_colon.

Upvotes: 0

Related Questions