getting mouseclick coordinates with Xlib

不羁的心 提交于 2019-11-29 11:47:04

You'll probably need to grab the pointer.

I don't know if you want just want button presses of releases. I've changed it to presses, but you can pick up both with:

XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ;

and add the ButtonRelease case back in.

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

int main (){
    int x=-1,y=-1;
    XEvent event;
    int button;
    Display *display = XOpenDisplay(NULL);
    if (display == NULL) {
    fprintf(stderr, "Cannot connect to X server!\n");
    exit (EXIT_FAILURE);
    }
    Window root= XDefaultRootWindow(display);
    XGrabPointer(display, root, False, ButtonPressMask, GrabModeAsync,
         GrabModeAsync, None, None, CurrentTime);

    XSelectInput(display, root, ButtonPressMask) ;
    while(1){
    XNextEvent(display,&event);
    switch(event.type){
    case ButtonPress:
        switch(event.xbutton.button){
        case Button1:
        x=event.xbutton.x;
        y=event.xbutton.y;
        button=Button1;
        break;

        case Button3:
        x=event.xbutton.x;
        y=event.xbutton.y;
        button=Button3;
        break;
        default:
        break;

        }
        break;
    default:
        break;
    }
    if(x>=0 && y>=0)break;
    }
    if(button==Button1)printf("leftclick at %d %d \n",x,y);
    else printf("rightclick at %d %d \n",x,y);
    XCloseDisplay(display);
    return 0;
}

I know this is a couple years out so this is informational for others coming along. First, using yet another library does not fit my definition of easier. I was working on something that needed this myself using xlib and C only and I wanted to see how simple I could make the code.

Essentially you only need four lines of code to do this in xlib in an existing program the rest is all in how you want to deal with it, printf/sprintf, grab_pixel_color(Display *display, XColor *color) etc.

/** gcc position.c -o position -lX11 **/
#include <X11/Xlib.h>
#include <stdio.h>

int main (int argc, char **argv) {
    Window root_window; //<--one
    int root_x, root_y; //<--two
    unsigned int mask; //<--three

    Display *display = XOpenDisplay(NULL);

    /*
    Bool XQueryPointer(display, w, root_return, child_return, root_x_return, root_y_return,
                         win_x_return, win_y_return, mask_return)
          Display *display;
          Window w;
          Window *root_return, *child_return;
          int *root_x_return, *root_y_return;
          int *win_x_return, *win_y_return;
          unsigned int *mask_return;
    */

    XQueryPointer(display, DefaultRootWindow(display), &root_window, &root_window, &root_x, &root_y, &root_x, &root_y, &mask); //<--four

    printf("Mouse coordinates (X: %d, Y: %d)\n", root_x, root_y);

    XCloseDisplay(display);
    return 0;
}

I hope this is useful to someone else.

If there is simpler way to do this in C, I am happy to hear it.

Perfect! Then use the libxdo library that some folks have already made for you.

int x, y, scrn;

xdo_t *hndl = xdo_new(NULL);
xdo_mouselocation(hndl, &x, &y, &scrn);
xdo_free(hndl);

printf("Mouse coordinates: x = %4d, y = %4d\n", x, y);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!