C 使用Xlib获取鼠标单击坐标

C 使用Xlib获取鼠标单击坐标,c,x11,xlib,C,X11,Xlib,我想知道如何在屏幕上的任何位置获得Xlib鼠标点击的x和y坐标。我找到了这篇文章,它得到了当前的指针位置 , 但我不知道如何修改它,以便在单击鼠标时获得x-y坐标。 我试着写这段代码,但它什么也没做 #include <stdio.h> #include <stdlib.h> #include <X11/Xlib.h> #include <X11/Xutil.h> int main (){ int x=-1,y=-1; XEvent event;

我想知道如何在屏幕上的任何位置获得Xlib鼠标点击的x和y坐标。我找到了这篇文章,它得到了当前的指针位置

,

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

#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);
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;
}
如果有更简单的方法在C中实现这一点,我很高兴听到它

如果有更简单的方法在C中实现这一点,我很高兴听到它

太好了!然后使用一些人已经为您制作的

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

您可能需要抓住指针

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

XSelectInput(display, root, ButtonPressMask|ButtonReleaseMask) ;
然后将按钮释放盒添加回

#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;
}
#包括
#包括
#包括
#包括
int main(){
int x=-1,y=-1;
XEvent事件;
int按钮;
Display*Display=XOpenDisplay(空);
如果(显示==NULL){
fprintf(stderr,“无法连接到X服务器!\n”);
退出(退出失败);
}
窗口根=XDefaultRootWindow(显示);
XGrabPointer(显示、根、假、按钮提示、GrabModeAsync、,
GrabModeAsync、None、None、CurrentTime);
XSelectInput(显示、根目录、按钮提示);
而(1){
XNextEvent(显示和事件);
开关(事件类型){
外壳按钮按下:
开关(event.xbutton.button){
案例按钮1:
x=event.xbutton.x;
y=event.xbutton.y;
按钮=按钮1;
打破
案例按钮3:
x=event.xbutton.x;
y=event.xbutton.y;
按钮=按钮3;
打破
违约:
打破
}
打破
违约:
打破
}
如果(x>=0&&y>=0)中断;
}
如果(button==Button1)printf(“在%d%d\n,x,y处左键单击”);
else printf(“右键单击%d%d\n”,x,y);
XCloseDisplay(显示);
返回0;
}

我知道这是几年后的事了,所以这是给其他人的信息。首先,使用另一个库不符合我对“更容易”的定义。我自己也在用xlib和C开发一些需要的东西,我想看看我能把代码做得多么简单

基本上,在现有程序的xlib中,您只需要四行代码就可以完成这项工作,其余的都取决于您希望如何处理它,printf/sprintf、grab_pixel_color(Display*Display、XColor*color)等

/**gcc位置.c-o位置-lX11**/
#包括
#包括
int main(int argc,字符**argv){

Window root_Window;//谢谢。该库肯定比xlib本身更易于使用,非常适合我的项目。但我没有看到任何函数在单击鼠标时返回坐标,因为您的示例在调用时只返回坐标。谢谢,代码对我来说很好,但我必须更改为ButtonRelease otherwise我得到了与上述相同的错误。你不知道为什么?这两个事件对我都有效。你使用的是哪个窗口管理器?我使用的是KDE窗口管理器。我做了一些研究,你的问题似乎是其他进程已经从根窗口捕获了按钮按下事件,我可以通过运行2来重现这个问题你的程序实例(第二个给出了这个错误)。没有额外的LIB,直截了当地说:未完成的答案(不管OPs请求的事件)。请不要从这个答案中删除任何内容。
#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;
}
/** 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;
}