xcb_grab_keyboard(3)
π xcb_grab_keyboard β XCB Requests β¨
okay bestie, pull up a chair, we're doing xcb_grab_keyboard from XCB Requests today and it's genuinely iconic π«Ά this is the one where you just. take the whole keyboard. all of it. no cap.
πΈ NAME πΈ
xcb_grab_keyboard β grab the keyboard π
β¨ SYNOPSIS β¨
First things first, you gotta invite it in:
#include <xcb/xproto.h>
π Request function
xcb_grab_keyboard_cookie_t xcb_grab_keyboard(xcb_connection_t *conn,
uint8_t owner_events, xcb_window_t grab_window,
xcb_timestamp_t time, uint8_t pointer_mode,
uint8_t keyboard_mode);
π¦ Reply datastructure
typedef struct xcb_grab_keyboard_reply_t {
uint8_t response_type;
uint8_t status;
uint16_t sequence;
uint32_t length;
} xcb_grab_keyboard_reply_t;
π Reply function
xcb_grab_keyboard_reply_t
*xcb_grab_keyboard_reply(xcb_connection_t *conn,
xcb_grab_keyboard_cookie_t cookie, xcb_generic_error_t **e);
π REQUEST ARGUMENTS π
these are the lil guys you hand over when you call it, let's go one by one:
connβ your XCB connection to X11. the whole vibe depends on it.owner_eventsβ if you set it to1, thegrab_windowstill gets the pointer events. if0, events are not reported to thegrab_window. simple as that.grab_windowβ specifies the window on which the pointer should be grabbed. (yes bestie the docs literally say pointer here, i'm not editing it, i'm a faithful narrator π)timeβ a timestamp, and it's there to dodge race conditions when X is running over the network. the special valueXCB_CURRENT_TIMEgets swapped out for the current server time. very "i'll be there when you need me" of it.pointer_modeβ one of these two values:XCB_GRAB_MODE_SYNCβ the state of the keyboard appears to freeze: no further keyboard events are generated by the server until the grabbing client issues a releasingAllowEventsrequest or until the keyboard grab is released. dramatic much? πXCB_GRAB_MODE_ASYNCβ keyboard event processing continues normally. the chill option.
keyboard_modeβ also one of these two values:XCB_GRAB_MODE_SYNCβ the state of the keyboard appears to freeze: no further keyboard events are generated by the server until the grabbing client issues a releasingAllowEventsrequest or until the keyboard grab is released.XCB_GRAB_MODE_ASYNCβ keyboard event processing continues normally.
fr fr,pointer_modeandkeyboard_modelist the exact same two values with the exact same descriptions. that's just how the page is, i'm not merging them, we keep every crumb π
π REPLY FIELDS π
what comes back in the reply, bestie:
response_typeβ the type of this reply, in this caseXCB_GRAB_KEYBOARD. this field is also present in thexcb_generic_reply_tand can be used to tell replies apart from each other. very "which one of y'all is this" energy.sequenceβ the sequence number of the last request processed by the X11 server.lengthβ the length of the reply, in words (a word is 4 bytes).statusβ one of these values:XCB_GRAB_STATUS_SUCCESSβ TODO: NOT YET DOCUMENTED.XCB_GRAB_STATUS_ALREADY_GRABBEDβ TODO: NOT YET DOCUMENTED.XCB_GRAB_STATUS_INVALID_TIMEβ TODO: NOT YET DOCUMENTED.XCB_GRAB_STATUS_NOT_VIEWABLEβ TODO: NOT YET DOCUMENTED.XCB_GRAB_STATUS_FROZENβ TODO: NOT YET DOCUMENTED.- TODO: NOT YET DOCUMENTED.
π not the stray second TODO: NOT YET DOCUMENTED. hanging out there with no name and no label. it's giving "left on read". it's in the original, so it's staying in here. no edits, no notes.
π DESCRIPTION π
Actively grabs control of the keyboard and generates FocusIn and FocusOut events. Further key events are reported only to the grabbing client. main character energy, truly β everyone else is in the group chat but only you can type π₯
Any active keyboard grab by this client is overridden. And now the part where it can say no to you, so pay attention bestie:
- if the keyboard is actively grabbed by some other client,
AlreadyGrabbedis returned. - if
grab_windowis not viewable,GrabNotViewableis returned. - if the keyboard is frozen by an active grab of another client,
GrabFrozenis returned. - if the specified
timeis earlier than the last-keyboard-grab time or later than the current X server time,GrabInvalidTimeis returned. - otherwise, the last-keyboard-grab time is set to the specified
time. slay, you got it β¨
π¦ RETURN VALUE π¦
Returns an xcb_grab_keyboard_cookie_t. Errors have to be handled when calling the reply function xcb_grab_keyboard_reply.
If you want to handle errors in the event loop instead, use xcb_grab_keyboard_unchecked. See xcb-requests(3) for details.
π ERRORS π
xcb_value_error_tβ TODO: reasons?xcb_window_error_tβ the specified window does not exist.
yes the first one literally asks "TODO: reasons?" in the source. we respect the source. we do not fix the source. we simply vibe with it π
π§ EXAMPLE π§
here's the whole thing in action, verbatim, no notes:
/*
* Grabs the keyboard actively
*
*/
void my_example(xcb_connection_t *conn, xcb_screen_t *screen) {
xcb_grab_keyboard_cookie_t cookie;
xcb_grab_keyboard_reply_t *reply;
cookie = xcb_grab_keyboard(
conn,
true, /* report events */
screen->root, /* grab the root window */
XCB_CURRENT_TIME,
XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
XCB_GRAB_MODE_ASYNC
);
if ((reply = xcb_grab_keyboard_reply(conn, cookie, NULL))) {
if (reply->status == XCB_GRAB_STATUS_SUCCESS)
printf("successfully grabbed the keyboard\n");
free(reply);
}
}
look at that XCB_GRAB_MODE_ASYNC showing up twice like a matching set. coordinated. ate. π
π SEE ALSO π
xcb-requests(3), xcb-examples(3), xcb_grab_pointer(3)
π AUTHOR π
Generated from xproto.xml. Contact xcb@lists.freedesktop.org for corrections and improvements.
okay that's the whole page bestie, go forth and grab keyboards responsibly π«Άβ¨
X Version 11 Β· libxcb 1.17.0 Β· xcb_grab_keyboard(3) β mwah, that's a wrap, love u, bye πππ¦
π¦ where this page comes from
- alpine 3.22: libxcb-doc 1.17.0-r0 Β· MIT
- alpine 3.23: libxcb-doc 1.17.0-r1 Β· MIT
- alpine 3.24: libxcb-doc 1.17.0-r2 Β· MIT
- arch rolling: libxcb 1.17.0-1 Β· X11
- centos-stream 10: libxcb-devel 1.17.0-3.el10 Β· X11
- debian trixie: libxcb-doc 1.17.0-2
- fedora 43: libxcb-devel 1.17.0-6.fc43 Β· X11
- fedora 43: libxcb-devel 1.17.0-6.fc43 Β· X11
- fedora 44: libxcb-devel 1.17.0-7.fc44 Β· X11
- fedora 44: libxcb-devel 1.17.0-7.fc44 Β· X11
- nixpkgs unstable: libxcb 1.17.0
- opensuse-leap 16.0: libxcb-devel-doc 1.17.0-160000.2.2 Β· MIT
- ubuntu questing: libxcb-doc 1.17.0-2build1
- ubuntu resolute: libxcb-doc 1.17.0-2ubuntu1
- ubuntu stonking: libxcb-doc 1.17.0-2ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms