以下代码片段在大多数情况下都有效,除了在某些窗口中。例如,在最新的 Ubuntu 下,它无法在文件资源管理器中选择文件夹。它似乎在其他地方都适用,但这个差距是巨大的。我怀疑这与我使用 XQueryPointer 的方式有关,但我已经尝试了几乎所有我能找到的示例。如果我改用电脑的鼠标,那就没问题了。
仅供参考:我已经尝试过这些问题的答案:发送程序化事件 https://stackoverflow.com/questions/4402216/sending-programtic-mouse-events-to-x
捕获鼠标输入 https://stackoverflow.com/questions/2607010/linux-how-to-capture-screen-and-simulate-mouse-movements但它们的工作原理并没有什么不同...
这是代码:
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
void SendClick(int button, int down) {
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
return;
}
memset(&event, 0, sizeof(event));
event.xbutton.button = button;
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow (display);
while (event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer (display, event.xbutton.window,
&event.xbutton.root, &event.xbutton.subwindow,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
event.type = down ? ButtonPress : ButtonRelease;
XSendEvent(display, PointerWindow, True, down ? ButtonPressMask : ButtonReleaseMask, &event);
XFlush(display);
XCloseDisplay(display);
}
感谢 ninjalj 上面的评论让我走上了正轨。我不喜欢依赖扩展来执行此操作的想法以及它创建的额外依赖项,但它也是一个非常标准的扩展。工作完美...
对于那些遇到与我相同问题的人,以下代码块替换了我之前使用的代码并且运行良好:
#include <X11/extensions/XTest.h>
void SendClick(int button, Bool down) {
Display *display = XOpenDisplay(NULL);
XTestFakeButtonEvent(display, button, down, CurrentTime);
XFlush(display);
XCloseDisplay(display);
}
矮得多!
对于 Ubuntu,不要忘记安装 libxtst-dev 软件包。请务必将 -lXtst 添加到您的 LDFLAGS。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)