daisy
daisy

Reputation: 23541

Qt: monitor global cursor click event with X11?

I want to capture global mouse click event in X11 , now

I tried to install a x11event filter , but it just doesn't work globally.

class XApplication: public QApplication
{
  public:
    XApplication (int & argc, char **argv):
        QApplication (argc , argv)
    {
    }

protected:
    bool x11EventFilter (XEvent *e)
    {
        qDebug() << "X11 Event: " << e->type;
        return QApplication::x11EventFilter(e);
    }
};

UPDATE

I mean outside the window , the code above works when I click on the window.

Upvotes: 1

Views: 3384

Answers (1)

pzanoni
pzanoni

Reputation: 3039

You can query X11 info from Qt using the QX11Info class. See its documentation. Then you can use raw Xlib from it.

You can use XGrabPointer(). If you use it, other apps won't receive the pointer events while the pointer is grabbed. man XGrabPointer will help you.

The "normal" way of subscribing for events is to use XSelectInput() on a window, but the problem is that you'll have to call XSelectInput on every single existing window. See its man page...

I know the xxf86dga extension has some calls related to mouse, but I'm not sure what they do.

XQueryPointer() is another way to query the pointer state without stealing events from other windows.

The only other place I can think of is the XInput extension, but I'm not sure it will help you either.

See the xev source code for a good reference on handling X11 events: http://cgit.freedesktop.org/xorg/app/xev

Sample code using XGrabPointer:

#include <stdio.h>
#include <assert.h>
#include <X11/Xlib.h>

int main(void)
{
        Display *d;
        Window root;

        d = XOpenDisplay(NULL);
        assert(d);

        root = DefaultRootWindow(d);

        XGrabPointer(d, root, False, ButtonPressMask | ButtonReleaseMask |
                     PointerMotionMask, GrabModeAsync, GrabModeAsync, None,
                     None, CurrentTime);

        XEvent ev;
        while (1) {                     
                XNextEvent(d, &ev);  
                switch (ev.type) { 
                case ButtonPress:
                        printf("Button press event!\n");
                        break;
                case ButtonRelease:
                        printf("Button release event!\n");
                        break;
                case MotionNotify:
                        printf("Motion notify event!\n");
                        break;
                default:
                        printf("Unknown event...\n");
                } 
        } 

        XCloseDisplay(d);
        return 0;
}

Compiled using: gcc x11mouse.c -o x11mouse -lX11

Upvotes: 3

Related Questions