Discussion:
[Xcb] XCB keyboard input
Chuck Pergiel
2017-03-08 14:34:30 UTC
Permalink
I've been working through the tutorial
<https://xcb.freedesktop.org/tutorial/> and everything was going swimmingly
until I got to the bit about keyboard input
<https://xcb.freedesktop.org/tutorial/fonts/>. Pressing the ESC key sends a
value of 9 to the program, which is great, but how did the ESC key get
translated to 9? The section of the program dealing with keyboard input is
listed below.

I can enter characters at the keyboard, but the values that show up in the
program are not the normal ASCII values. I've dug around a bit but I
haven't found much of anything that was very helpful.

If you can offer any advice, I would appreciate hearing from you.

Chuck Pergiel
Silicon Forest
www.pergelator.blogspot.com
Sent from my Commodore-64 via a US Robotics 300 Baud Modem

P.S. I'm running Linux Mint on an HP desktop PC, if that makes any
difference.

/* event loop */
xcb_generic_event_t *event;
while (1) { ;
if ( (event = xcb_poll_for_event(connection)) ) {
switch (event->response_type & ~0x80) {
case XCB_EXPOSE: {
drawText (connection,
screen,
window,
10, HEIGHT - 10,
"Press ESC key to exit..." );
break;
}
case XCB_KEY_RELEASE: {
xcb_key_release_event_t *kr =
(xcb_key_release_event_t *)event;

switch (kr->detail) {
/* ESC */
case 9: {
free (event);
xcb_disconnect (connection);
return 0;
}
}
free (event);
}
}
}
}
Ran Benita
2017-03-08 15:32:01 UTC
Permalink
The 9 you get is called a keycode, from your description what you want
is the keysym, ands its ASCII representation.

Look at the xcb-util-keysyms helper library; it is small and uses the
core X11 protocol:
https://cgit.freedesktop.org/xcb/util-keysyms/tree/keysyms/xcb_keysyms.h

If you need more advanced handling, you can use libxkbcommon-x11; this
library uses the XKB extension:
https://xkbcommon.org/doc/current/md_doc_quick-guide.html
https://xkbcommon.org/doc/current/group__x11.html

Both libraries are available as packages in all common distros.

Ran
Peter Harris
2017-03-08 15:45:11 UTC
Permalink
I've been working through the tutorial and everything was going
swimmingly until I got to the bit about keyboard input.
Pressing the ESC key sends a value of 9 to the program, which is great,
but how did the ESC key get translated to 9? The section of the program
dealing with keyboard input is listed below.
The section of the program you quoted does not translate the ESC key to
9. It relies on luck, mostly, and the fact that historically it happens
to be 9 on most x servers.
I can enter characters at the keyboard, but the values that show up in
the program are not the normal ASCII values. I've dug around a bit but I
haven't found much of anything that was very helpful.
You need to translate the keycode (plus modifiers, like shift) into a
key symbol. xcb_get_keyboard_mapping is rather sparsely documented at
https://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html#requests:GetKeyboardMapping
. See https://cgit.freedesktop.org/xcb/util-keysyms/ for example code.

The key symbols predate unicode (and have to encode symbols that are
outside of unicode anyway, like the F1-F12 keys on the keyboard). The
definitions are in the *keysym.h files in
https://cgit.freedesktop.org/xorg/proto/x11proto/

You can run `xev` and press keys on your keyboard to see what is going on.
Sent from my Commodore-64 via a US Robotics 300 Baud Modem
Luxury! I bet that even supports the standard Hayes command set. My
Commodore-64 has a no-name 300 baud modem with hardware toggle switches
on the front (no software dialing for me).

Peter Harris
--
Open Text Connectivity Solutions Group
Peter Harris http://connectivity.opentext.com/
Research and Development Phone: +1 905 762 6001
***@opentext.com Toll Free: 1 877 359 4866
Loading...