[C / C ] Getting mouse events in C on Ubuntu – Mouse event C on Ubuntu

To capture mouse events in ubuntu we use the library X11 / Xlib.h. Install this library with the command:

sudo apt-get install libx11-dev

This is an illustration from the program page 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 mice in linux

However, I still do not know how to use it in graphics. We hope you share on this issue.

Refer: Click in Dev-C