假设我知道窗口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的情况下发送鼠标单击?

最佳答案

使用XTestFakeMotionEventXTEST扩展

关于linux - x11-在不移动指针的情况下发送鼠标单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27984220/

10-13 05:45