X11 XSendEvent does not click mouse

Post backdated by a year to keep it off the front page. Don’t know of any other way.

Updates: See End of Post

Building a Qt application that spawns another app and then clicks on it – the top window I think. Got it working on Windows with SendInput.

Now for the same idea on Linux. I’m trying to use X11. Found lots of examples using XQueryPointer and XSendEvent. Odd loop looking through windows and subwindows I have no clue about. Anyway, it doesn’t work and I have no idea which window the code is trying to target. Nobody talks about that in the examples.

Code runs, checks for errors and finds none. But clicks do not work. My click on mouse works instantly.

How do I figure out where clicks are going? And how do I find the top window or whatever I need to send clicks there?

Here’s the debug output of the app:

Screenshot from 2020-04-09 12-27-58.png

Here are the routines called by the Qt application from TimerEvents inside the QtWidget application. Here is the code over on GitHub.

X11MouseClick Source On GitHub

Let’s try it as a Gist. Whooo hoooo…..

// Let's try the more straightforward X11 API to click the mouse
// This example code was harvested from the internet
// https://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/
// Another link is here
// https://stackoverflow.com/questions/27984220/x11-sending-a-mouse-click-without-moving-a-pointer
#include <QMainWindow>
#include <QDebug>
#include <QString>
#include <mouseclick.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
// open and close do nothing.
// we are going to open the display each time we click.
bool mouseopen()
return true;
bool mouseclose()
return true;
bool mouseclick()
int button = Button1; // just click button one. I'm sure it will be fine.
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
qDebug() << QString("XOpenDisplay returned NULL");
return false;
//fprintf(stderr, "Errore nell'apertura del Display !!!\n");
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)),
&event.xbutton.root, &event.xbutton.window,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
qDebug() << "Mouse Window " << event.xbutton.window;
#if 1 // don't look for a subwindow. Just use display. Is this Root?
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);
qDebug() << "Mouse Subwindow" << event.xbutton.window;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
qDebug() << QString("XSendEvent error XS1");
//fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
qDebug() << "Button Press";
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
qDebug() << QString("XSendEvent error XS2");
//fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
qDebug() << "Button Release";
return true;

Super that works too. Who knew.

Ok, so how do I figure out what’s wrong here so my code can click’ity click click on other applications?



Tried Keypress events rather than mouse clicks and used XGetInputFocus to find the window. No success. Here is the github page for latest code:

GitHub page for X11MouseClick.cpp


Qt Linking with X11?

Dated a year ago to get it off the front page.

Qt Application on Linux Mint trying to link with X11 libraries. Original errors are:

Screenshot from 2020-04-09 10-15-35.png

So it looks like it can’t find XFlush. Maybe can’t find libX11.so. Added this to the project file:

Screenshot from 2020-04-09 10-17-22.png

But still got this error:

Screenshot from 2020-04-09 10-16-33.png

But libX11.so is clearly present on system:

Screenshot from 2020-04-09 10-21-19.png

So how do I tell Qt to find libX11.so and link with it?