gpt4 book ai didi

c - Linux X11 C 想知道屏幕上任何地方的指针运动

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:26 24 4
gpt4 key购买 nike

我想编写一个小窗口程序,在屏幕上的任何位置显示指针位置(和其他信息)。我不想在所有其他运行的程序中中断鼠标的正常使用。有没有一种方法可以“ Hook ”指针运动事件,或者我如何设置一个计时器事件来轮询我的事件循环中的指针位置?

最佳答案

我想向您指出 Xautolock code queryPointer function , 它计算出鼠标的位置。如果您深入研究 main.cdiy.c,您还应该获得足够的信息,了解根据鼠标光标位置和空闲超时采取什么行动是必要的。

实际上,它只使用 XQueryPointer获取信息。但是,您应该记住所有鼠标移动都是相对的,因此您必须根据初始位置保持状态。示例代码可在 another SO thread 中找到.

编辑

由于从 Xautolock 代码来看它不是很明显,所以我抄写了一些我认为你正在寻找的东西。这是一组最小的代码,仅将 x/y 坐标转储到标准输出。如果你想静态地使用它们,你必须创建并定位一个窗口来绘制一些文本。像这段代码一样以 1 毫秒的分辨率进行轮询,该程序在我的机器上大约占用 0.1% 的 CPU(Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz)。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>

static Window root;

static void
query_pointer(Display *d)
{
static int once;
int i, x, y;
unsigned m;
Window w;

if (once == 0) {
once = 1;
root = DefaultRootWindow(d);
}

if (!XQueryPointer(d, root, &root, &w, &x, &y, &i, &i, &m)) {
for (i = -1; ++i < ScreenCount(d); ) {
if (root == RootWindow(d, i)) {
break;
}
}
}

fprintf(stderr, "X: %d Y: %d\n", x, y);
}

static int
catchFalseAlarm(void)
{
return 0;
}

int
main(void)
{
XSetWindowAttributes attribs;
Display* d;
Window w;

if (!(d = XOpenDisplay(0))) {
fprintf (stderr, "Couldn't connect to %s\n", XDisplayName (0));
exit (EXIT_FAILURE);
}

attribs.override_redirect = True;
w = XCreateWindow(d, DefaultRootWindow (d), -1, -1, 1, 1, 0,
CopyFromParent, InputOnly, CopyFromParent,
CWOverrideRedirect, &attribs);
XMapWindow(d, w);
XSetErrorHandler((XErrorHandler )catchFalseAlarm);
XSync (d, 0);

for (;;) {
query_pointer(d);
usleep(1000);
}

return 0;
}

关于c - Linux X11 C 想知道屏幕上任何地方的指针运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27722770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com