我的目标是实现一个简单地返回当前鼠标位置的程序(不打开任何窗口,无论正在运行什么)。在搜索了这个之后,我能找到的最接近和最简单的实现是通过autopy,一个用于做这类事情的 python 库。
返回当前鼠标位置的函数“get_pos()”。请参阅此处的文档。我只是想自己实现'get_pos()'函数(因为这就是我需要从我正在开发的程序的autopy中包含的全部内容)。
在一个 github repo 中,我搜索了 autopy 的源代码,得出以下结论。调用 'get_pos()' 会导致 *mouse_get_pos 函数发生(请参见此处的完整代码):
/* Syntax: get_pos() => tuple (x, y) */
/* Description: Returns a tuple `(x, y)` of the current mouse position. */
static PyObject *mouse_get_pos(PyObject *self, PyObject *args);
这个函数似乎调用了“getMousePos”:
static PyObject *mouse_get_pos(PyObject *self, PyObject *args)
{
MMPoint pos = getMousePos();
return Py_BuildValue("kk", pos.x, pos.y);
}
位于mouse.c头文件中:
MMPoint getMousePos()
{
#if defined(IS_MACOSX)
CGEventRef event = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(event);
CFRelease(event);
return MMPointFromCGPoint(point);
#elif defined(USE_X11)
int x, y; /* This is all we care about. Seriously. */
Window garb1, garb2; /* Why you can't specify NULL as a parameter */
int garb_x, garb_y; /* is beyond me. */
unsigned int more_garbage;
Display *display = XGetMainDisplay();
XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2,
&x, &y, &garb_x, &garb_y, &more_garbage);
return MMPointMake(x, y);
#elif defined(IS_WINDOWS)
POINT point;
GetCursorPos(&point);
return MMPointFromPOINT(point);
#endif
}
这段代码似乎包括一种在所有操作系统中返回鼠标位置的方法,这是我试图在我的程序中实现的。我怎样才能让这个 c 函数在我的系统上正常工作,以便我可以在我的程序中实现它?
编辑:我尝试使用'gcc mouse.c'简单地编译mouse.c,我收到了这个错误:
Undefined symbols for architecture x86_64:
"_CFRelease", referenced from:
_moveMouse in ccVkh5f7.o
_getMousePos in ccVkh5f7.o
_toggleMouse in ccVkh5f7.o
"_CGEventCreate", referenced from:
_getMousePos in ccVkh5f7.o
"_CGEventCreateMouseEvent", referenced from:
_moveMouse in ccVkh5f7.o
_toggleMouse in ccVkh5f7.o
"_CGEventGetLocation", referenced from:
_getMousePos in ccVkh5f7.o
"_CGEventPost", referenced from:
_moveMouse in ccVkh5f7.o
_toggleMouse in ccVkh5f7.o
"_deadbeef_rand", referenced from:
_smoothlyMoveMouse in ccVkh5f7.o
"_getMainDisplaySize", referenced from:
_smoothlyMoveMouse in ccVkh5f7.o
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
这是否使任何潜在的问题变得明显?