简体   繁体   中英

XY Coordinate of cursor on click in X11

I've looked through some of the docs but can't find a nice description of how to get the cursor's position (in terms of pixels from top-left 0,0) in the window on click.

Any help is appreciated :D

Here's a minimal program that reports where the mouse is clicked. It just picks up the Button Press event which contains the x,y (top-left is 0,0) position of the mouse pointer in the window.

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

int main(int argc, char**argv)
{
    Display *display = XOpenDisplay(NULL);

    Window window = XCreateSimpleWindow(
        display, RootWindow(display,0), 0, 0,
        600, 600, 1, BlackPixel(display, 0),
        WhitePixel(display, 0));

    XMapWindow(display, window);

    XSelectInput(display, window, ButtonPressMask);

    XEvent event;
    while (1)
    {
        XNextEvent(display, &event);
        switch  (event.type) {
        case ButtonPress:
            printf("Clicked at %d,%d\n", event.xbutton.x, event.xbutton.y);
            break;
        }
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM