我们假设我知道窗口ID。然后我可以通过这种方式向该窗口发送鼠标点击(它是一个全屏窗口,因此根和窗口坐标是相同的):
Display *display = XOpenDisplay(NULL);
if(display == NULL)
{
std::cout<<"Cannot open display"<<std::endl;
exit(EXIT_FAILURE);
}
XWarpPointer(display, None, RootWindow(display, DefaultScreen(display)), 0, 0, 0, 0, 300, 200);
XEvent event;
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
event.xbutton.root = RootWindow(display, DefaultScreen(display));
event.xbutton.window = 75497473;
event.xbutton.subwindow = 0;
event.xbutton.x_root = 300;
event.xbutton.y_root = 200;
event.xbutton.x = 300;
event.xbutton.y = 200;
event.xbutton.state = 0;
XSendEvent(display, PointerWindow, True, ButtonPressMask, &event) == 0)
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
XSendEvent(display, PointerWindow, True, ButtonReleaseMask, &event) == 0)
XFlush(display);
如果我删除了该代码,则该代码无效:
XWarpPointer(display, None, RootWindow(display, DefaultScreen(display)), 0, 0, 0, 0, 300, 200);
我没有使用XQueryPointer,所以我不明白哪里出了问题。是否可以在不使用XWarpPointer的情况下发送鼠标?