With vanilla XCB
(ie. without extensions like XTEST
), using xcb_send_event()
:
xcb_screen_t* xcb_get_screen(xcb_connection_t* connection, int screen_idx){ // Return a screen from its number!
const xcb_setup_t* setup = xcb_get_setup(connection);
for(xcb_screen_iterator_t screen_it = xcb_setup_roots_iterator(setup); screen_it.rem; --screen_idx, xcb_screen_next(&screen_it))
if(screen_idx==0) return screen_it.data;
return NULL;
}
fdefi void xcb_send_key_press(xcb_connection_t* connection, xcb_window_t root, xcb_window_t window, xcb_keycode_t keycode){ // http://t-sato.in.coocan.jp/xvkbd/events.html
xcb_key_press_event_t ev; memset(&ev,0x00,sizeof(xcb_key_press_event_t));
ev.response_type = XCB_KEY_PRESS;
ev.detail = keycode;
ev.time = XCB_CURRENT_TIME;
ev.root = root;
ev.event = window;
ev.state = 0x0000; // xcb_mod_mask_t: XCB_MOD_MASK_SHIFT XCB_MOD_MASK_LOCK XCB_MOD_MASK_CONTROL XCB_MOD_MASK_1 XCB_MOD_MASK_2 XCB_MOD_MASK_3 XCB_MOD_MASK_4 XCB_MOD_MASK_5 XCB_MOD_MASK_ANY
ev.same_screen = 1;
xcb_send_event(connection, 0/*1*/, window/*XCB_SEND_EVENT_DEST_ITEM_FOCUS*/, XCB_EVENT_MASK_NO_EVENT/*XCB_EVENT_MASK_KEY_PRESS XCB_EVENT_MASK_NO_EVENT*/, (const char*)&ev); // @XCB_SEND_EVENT_DEST_ITEM_FOCUS needs to have @propate set to 1? // every X11 event is 32 bytes
}
int main(){ // תִּשְׂמָ֑ח
int xcb_screen_idx;
xcb_connection_t* xcb_connection = xcb_connect(NULL,&xcb_screen_idx);
xcb_screen_t* xcb_screen = xcb_get_screen(xcb_connection,xcb_screen_idx); // xcb_meta(xcb_connection,xcb_screen_idx);
xcb_get_input_focus_cookie_t get_input_focus_cookie = xcb_get_input_focus_unchecked(xcb_connection);
xcb_get_input_focus_reply_t* get_input_focus_reply = xcb_get_input_focus_reply(xcb_connection, get_input_focus_cookie, NULL);
xcb_send_key_press(xcb_connection, xcb_screen->root, get_input_focus_reply->focus, 0x26);
free(get_input_focus_reply);
xcb_disconnect(xcb_connection);
}