[C / C ] 获取鼠标事件C对Ubuntu的 – 在Ubuntu鼠标事件Ç

为了捕捉鼠标事件在Ubuntu中,我们使用的库X11 / Xlib.h. 设置该库以:

命令和apt-get安装的libx11-dev的

这是从节目页的图 http://stackoverflow.com/questions/14553435/how-to-listen-for-mouse-events-in-linux

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

char *key_name[] = {
    "first",
    "second (or middle)",
    "third",
    "fourth",  
    "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  : %sn", key_name[xevent.xbutton.button - 1]);
                break;
            case ButtonRelease:
                printf("Button released : %sn", key_name[xevent.xbutton.button - 1]);
                break;
        }
    }

    return 0;
}

C小鼠在Linux

不过,我还是不知道如何使用它的图形. 我们希望你在这个问题上分享.

参考: 单击开发-C