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

发布于 2024-09-16 17:53:15 字数 98 浏览 5 评论 0原文

这可以是一些示例 C 代码,也可以是一个实用程序,可以在 gui 或控制台上向我显示这并不重要,但我必须能够“命令”它在准确的时间获取坐标,这使得xev 不是很有用(我可以弄清楚)。

This can either be some sample C code or a utility that will show me either gui or on the console it doesn't matter, but I have to be able to "command" it to grab the co-ordinates at an exact time which makes xev not very useful (that I could figure out).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思念绕指尖 2024-09-23 17:53:15

无论如何,我不是 C 程序员,但我看了几个在线教程,认为这就是您应该读取当前鼠标位置的方式。这是我自己的代码,之前我没有对 Xlib 进行任何操作,因此它可能会被完全破坏(例如,错误处理程序不应该对每个错误不执行任何操作),但它可以工作。所以这是另一个解决方案:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>

static int _XlibErrorHandler(Display *display, XErrorEvent *event) {
    fprintf(stderr, "An error occured detecting the mouse position\n");
    return True;
}

int main(void) {
    int number_of_screens;
    int i;
    Bool result;
    Window *root_windows;
    Window window_returned;
    int root_x, root_y;
    int win_x, win_y;
    unsigned int mask_return;
    
    Display *display = XOpenDisplay(NULL);
    assert(display);
    XSetErrorHandler(_XlibErrorHandler);
    number_of_screens = XScreenCount(display);
    fprintf(stderr, "There are %d screens available in this X session\n", number_of_screens);
    root_windows = malloc(sizeof(Window) * number_of_screens);
    for (i = 0; i < number_of_screens; i++) {
        root_windows[i] = XRootWindow(display, i);
    }
    for (i = 0; i < number_of_screens; i++) {
        result = XQueryPointer(display, root_windows[i], &window_returned,
                &window_returned, &root_x, &root_y, &win_x, &win_y,
                &mask_return);
        if (result == True) {
            break;
        }
    }
    if (result != True) {
        fprintf(stderr, "No mouse found.\n");
        return -1;
    }
    printf("Mouse is at (%d,%d)\n", root_x, root_y);
    
    free(root_windows);
    XCloseDisplay(display);
    return 0;
}

I'm not a C programmer by any means but I looked at a couple of online tutorials and think this is how you are supposed to read the current mouse position. This is my own code and I'd done nothing with Xlib before so it could be completely broken (for example, the error handler shouldn't just do nothing for every error) but it works. So here is another solution:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>

static int _XlibErrorHandler(Display *display, XErrorEvent *event) {
    fprintf(stderr, "An error occured detecting the mouse position\n");
    return True;
}

int main(void) {
    int number_of_screens;
    int i;
    Bool result;
    Window *root_windows;
    Window window_returned;
    int root_x, root_y;
    int win_x, win_y;
    unsigned int mask_return;
    
    Display *display = XOpenDisplay(NULL);
    assert(display);
    XSetErrorHandler(_XlibErrorHandler);
    number_of_screens = XScreenCount(display);
    fprintf(stderr, "There are %d screens available in this X session\n", number_of_screens);
    root_windows = malloc(sizeof(Window) * number_of_screens);
    for (i = 0; i < number_of_screens; i++) {
        root_windows[i] = XRootWindow(display, i);
    }
    for (i = 0; i < number_of_screens; i++) {
        result = XQueryPointer(display, root_windows[i], &window_returned,
                &window_returned, &root_x, &root_y, &win_x, &win_y,
                &mask_return);
        if (result == True) {
            break;
        }
    }
    if (result != True) {
        fprintf(stderr, "No mouse found.\n");
        return -1;
    }
    printf("Mouse is at (%d,%d)\n", root_x, root_y);
    
    free(root_windows);
    XCloseDisplay(display);
    return 0;
}
岁月如刀 2024-09-23 17:53:15

xdotool 可能是最好的工具。

对于 C,您可以使用 libxdo

xdotool might be the best tool for this.

For C, you can use libxdo.

小情绪 2024-09-23 17:53:15

实际上,xev 非常有用,如果您向它提供使用 xwininfo 抓取的窗口 id,那么它可以轻松地为您执行此任务。毫无疑问,还有更优雅的解决方案,但它确实有效。

Actually, xev is very useful if you supply it with the window id grabbed using xwininfo, then it can easily perform this task for you. There are no doubt much more elegant solutions but it works.

你是我的挚爱i 2024-09-23 17:53:15

xinput 可用于打印任何输入设备的完整设备状态。

首先你需要发现你的设备ID:

$ xinput --list | grep -i mouse                                                                                                                                
⎜   ↳ Logitech USB Receiver Mouse               id=11   [slave  pointer  (2)]

然后你可以询问状态:

$ xinput --query-state 11;
2 classes :
ButtonClass
        button[1]=up
        button[2]=up
        button[3]=up
        button[4]=up
        button[5]=up
        button[6]=up
        button[7]=up
        button[8]=up
        button[9]=up
        button[10]=up
        button[11]=up
        button[12]=up
        button[13]=up
        button[14]=up
        button[15]=up
        button[16]=up
        button[17]=up
        button[18]=up
        button[19]=up
        button[20]=up
ValuatorClass Mode=Relative Proximity=In
        valuator[0]=274
        valuator[1]=886
        valuator[2]=0
        valuator[3]=675

或者只是一个循环:

while sleep .2; do xinput --query-state $(xinput --list | grep -i mouse | cut -d= -f2 | cut -f1| head -1); done

xinput can be used to print the full device state of any input device.

First you need to discover your device id:

$ xinput --list | grep -i mouse                                                                                                                                
⎜   ↳ Logitech USB Receiver Mouse               id=11   [slave  pointer  (2)]

then you can ask for state:

$ xinput --query-state 11;
2 classes :
ButtonClass
        button[1]=up
        button[2]=up
        button[3]=up
        button[4]=up
        button[5]=up
        button[6]=up
        button[7]=up
        button[8]=up
        button[9]=up
        button[10]=up
        button[11]=up
        button[12]=up
        button[13]=up
        button[14]=up
        button[15]=up
        button[16]=up
        button[17]=up
        button[18]=up
        button[19]=up
        button[20]=up
ValuatorClass Mode=Relative Proximity=In
        valuator[0]=274
        valuator[1]=886
        valuator[2]=0
        valuator[3]=675

Or just a loop:

while sleep .2; do xinput --query-state $(xinput --list | grep -i mouse | cut -d= -f2 | cut -f1| head -1); done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文