使用Xlib获取鼠标点击坐标

我想知道如何在屏幕上的任何位置使用Xlib获取鼠标点击的x和y坐标。 我发现这个post获取当前指针的位置

如何在X中获取当前鼠标(指针)位置坐标 ,

但我不知道如何修改它,以便在单击鼠标时获取xy坐标。 我试过写这段代码,但它什么也没做。

#include  #include  #include  #include  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); XSelectInput(display, root, ButtonReleaseMask) ; while(1){ XNextEvent(display,&event); switch(event.type){ case ButtonRelease: 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; } 

事件可能会发送到其他窗口,这就是它无法正常工作的原因。 另一个问题是,当我将ButtonPressMask添加到XSelectInput函数时,我收到以下错误:

 X Error of failed request: BadAccess (attempt to access private resource denied) Major opcode of failed request: 2 (X_ChangeWindowAttributes) Serial number of failed request: 7 Current serial number in output stream: 7 

如果在C中有更简单的方法,我很高兴听到它。

你可能需要抓住指针。

我不知道你是否只想按下发布按钮。 我已将其更改为印刷机,但您可以选择以下两种方式:

 XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ; 

并重新添加ButtonRelease案例。

 #include  #include  #include  #include  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; } 

如果在C中有更简单的方法,我很高兴听到它。

完善! 然后使用一些人已经为你做的libxdo库 。

 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); 

我知道这是几年了,所以这对其他人来说是有用的。 首先,使用另一个库不符合我更容易的定义。 我正在使用xlib和C来处理需要这些的东西,我想看看我能编写代码有多简单。

基本上你只需要四行代码就可以在现有程序的xlib中执行此操作,剩下的就是你要如何处理它,printf / sprintf,grab_pixel_color(Display * display,XColor * color)等。

 /** gcc position.c -o position -lX11 **/ #include  #include  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; } 

我希望这对其他人有用。