I have a small program that takes input in the form of ascii characters. I need to be able to convert these to keycodes for use with x11 functions. Is there a xlib function to do this or another library? Or will a large switch case work best?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
This question has an old, wrong answer (from @oldrinb), that oddly has never been challenged. As stated in the comment, you can't use XStringToKeysym to map chars to KeySyms in a general way. It will work for letters and numbers, but that's about it, because the KeySym name happens to map directly for those ASCII characters. For other ASCII characters such as punctuation or space it won't work.
But you can do better than that. If you look at
<X11/keysymdef.h>
you find that forASCII
0x20-0xFF, the characters map directly toXKeySyms
. So, I'd say it's simpler to just use that range of characters directly asKeySyms
, and just map the remaining 32 characters to their correspondingKeyCodes
. So I'd say the code should more properly be:The 'else' clause will require multiple
KeyCodes
since for exampleASCII
1 (Control-A) isXK_A
with theXK_CONTROL_R
(orXK_CONTROL_L
) Modifier. So you'd have to issue for example:XK_CONTROL_L
DOWN,XK_A
DOWN,XK_A
UP,XK_CONTROL_L
UP.Here's a toy program that demonstrates this by echoing the first argument via simulated keyboard events:
You need to link it with:
-lX11 -lxcb -lxcb-xtest -lX11-xcb
Disclaimer: No KeySyms were harmed in the writing of this code.
You can use
XStringToKeysym
to convert toKeySym
, followed byXKeysymToKeycode
for conversion toKeyCode
.