Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send key event to application using XCB?

Tags:

c++

c

x11

xcb

How can I send a key press or key release event to a window (the currently active window) from another program using XCB?

I found some tutorials using XLib, however I would like to use XCB.

I guess I will have to call xcb_send_event, however I have no idea what to pass it as parameters.

like image 982
ar31 Avatar asked Jan 09 '12 19:01

ar31


2 Answers

Possible alternative: xcb_send_event()

See: X11R7.7 API Documentation

Example from there:

/*
 * Tell the given window that it was configured to a size of 800x600 pixels.
 */
void my_example(xcb_connection_t* conn, xcb_window_t window) {

    //
    // Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
    // In order to properly initialize these bytes, we allocate 32 bytes even
    // though we only need less for an xcb_configure_notify_event_t
    //
    xcb_configure_notify_event_t* event = calloc(32, 1);

    event->event = window;
    event->window = window;
    event->response_type = XCB_CONFIGURE_NOTIFY;

    event->x = 0;
    event->y = 0;
    event->width = 800;
    event->height = 600;

    event->border_width = 0;
    event->above_sibling = XCB_NONE;
    event->override_redirect = false;

    xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*) event);
    xcb_flush(conn);
    free(event);
}
like image 157
Christian Heller Avatar answered Nov 13 '22 19:11

Christian Heller


You should be able to use the XTEST extension to fake input to the active window, using the xcb_test_fake_input() function.

#include <xcb/xtest.h>
...
xcb_test_fake_input(c, XCB_KEY_PRESS, keycode, XCB_CURRENT_TIME, XCB_NONE, 0, 0, 0);
xcb_test_fake_input(c, XCB_KEY_RELEASE, keycode, XCB_CURRENT_TIME, XCB_NONE, 0, 0, 0);

See the xte program in xcb/demos for a working example.

like image 36
jturney Avatar answered Nov 13 '22 20:11

jturney