ubuntu下拦截鼠标事件

ubuntu下拦截鼠标事件, 使用x11编程,代码如下:

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

char *key_name[] = {
    "first",
    "second (or middle)",
    "third",
    "fourth",  // :D
    "fivth"    // :|
};

int main(int argc, char **argv)
{
    Display *display;
    XEvent xevent;
    Window window;

    if( (display = XOpenDisplay(NULL)) == NULL )
        return -1;


    window = DefaultRootWindow(display);
    XAllowEvents(display, AsyncBoth, CurrentTime);

    XGrabPointer(display, 
                 window,
                 1, 
                 /*PointerMotionMask | */ButtonPressMask | ButtonReleaseMask , 
                 GrabModeAsync,
                 GrabModeAsync, 
                 None,
                 None,
                 CurrentTime);

    while(1) {
        XNextEvent(display, &xevent);

        switch (xevent.type) {
            case MotionNotify:
                printf("Mouse move      : [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
                break;
            case ButtonPress:
                printf("Button pressed  : %s\n", key_name[xevent.xbutton.button - 1]);
                break;
            case ButtonRelease:
                printf("Button released : %s\n", key_name[xevent.xbutton.button - 1]);
                break;
        }
    }

    return 0;
}

编译:gcc main.c -o mouse -lX11

注意:运行后鼠标将不能使用,关闭时使用键盘在终端输入ps -aux | grep mouse, 然后使用kill杀死mouse进程。

--------------------------------------------------------------

附x11和xcb的资源:

Xlib Manual: http://tronche.com/gui/x/xlib/

xcb: http://xcb.freedesktop.org/

An Introduction to XCB Programming: https://www.codeproject.com/articles/1089819/an-introduction-to-xcb-programming


使用Qt+x11+xcb的例子:http://download.csdn.net/download/xgbing/10274836


猜你喜欢

转载自blog.csdn.net/xgbing/article/details/79479380