Simulate a mouse click using X11

2

I'm trying to simulate a mouse click using the X11 library on linux. Using the xev command on the terminal I have the following outputs.

For a real click:

ButtonPress event, serial 32, synthetic NO, window 0x2c00001,
root 0xdb, subw 0x0, time 1776350, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES

ButtonRelease event, serial 32, synthetic NO, window 0x2c00001,
root 0xdb, subw 0x0, time 1776470, (302,342), root:(306,365),
state 0x110, button 1, same_screen YES

For the simulated click:

ButtonPress event, serial 32, synthetic YES, window 0x2c00001,
root 0xdb, subw 0x0, time 0, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES

ButtonRelease event, serial 32, synthetic YES, window 0x2c00001,
root 0xdb, subw 0x0, time 0, (302,342), root:(306,365),
state 0x10, button 1, same_screen YES

The problem is that the click does not happen, the event appears but the mouse does not click. I'm leaving the code below, I really do not think where the problem might be.

void MouseClick(int button)
{
    Display *display = XOpenDisplay(NULL);

    // Create and setting up the event
    XEvent event;
    memset(&event, 0, sizeof(event));
    event.xbutton.button = button;
    event.xbutton.same_screen = True;
    event.xbutton.subwindow = DefaultRootWindow(display);
    while (event.xbutton.subwindow)
    {
        event.xbutton.window = event.xbutton.subwindow;
        XQueryPointer(display, event.xbutton.window,
                      &event.xbutton.root, &event.xbutton.subwindow,
                      &event.xbutton.x_root, &event.xbutton.y_root,
                      &event.xbutton.x, &event.xbutton.y,
                      &event.xbutton.state);
    }
    // Press
    event.type = ButtonPress;
    if (XSendEvent(display, PointerWindow, True, ButtonPressMask, &event) == 0)
        fprintf(stderr, "Error to send the event!\n");
    XFlush(display);
    usleep(1);
    // Release
    event.type = ButtonRelease;
    if (XSendEvent(display, PointerWindow, True, ButtonReleaseMask, &event) == 0)
        fprintf(stderr, "Error to send the event!\n");
    XFlush(display);
    usleep(1);

    XCloseDisplay(display);
}
    
asked by anonymous 15.01.2018 / 03:51

0 answers