您可以通过将XMotionEvent发送到窗口来实现此目的。X11 知道它是一个拖动事件的方法是检查状态。因此,如果状态是与 Button1Mask 的 OR,则意味着当 Button1 被按下时动作完成。
我在这里粘贴伪代码。您可能希望根据您的用例填充 XMotionEvent 的相关字段。
Display *OpenedDisplay = XOpenDisplay(NULL);
int ret = 0,screen = 0;
XMotionEvent xbpe;
Window dummy;
unsigned int mask,dummyUInt;
int dummyInt;
int screencount = ScreenCount(xbpe.display);
for (int i = 0; i < screencount; i++) {
Screen *screenPointer = ScreenOfDisplay(xbpe.display, i);
ret = XQueryPointer(xbpe.display, RootWindowOfScreen(screenPointer), &dummy, &dummy,
&dummyInt, &dummyInt, &dummyInt, &dummyInt, &mask);
if (ret == True) {
screen = i;
break;
}
}
xbpe.type = MotionNotify;
xbpe.display = OpenedDisplay;
xbpe.window = wid;
xbpe.root = RootWindow(xbpe.display, screen);
xbpe.subwindow = None;
xbpe.time = CurrentTime;
xbpe.x = src_x;
xbpe.y = src_y;
xbpe.state = mask;
xbpe.is_hint = NotifyNormal;
xbpe.same_screen = True; /* Should we detect if window is on the same screen as cursor? */
XWindowAttributes attr;
ret = XGetWindowAttributes(xbpe.display, wid, &attr);
if(ret == 0){
qDebug() << wid <<"XGetWindowAttributes unsuccessfull returning early";
return;
}
/**************************** This is key here *******************/
if(isDrag){
xbpe.state |= Button1Mask;
}
ret = XSendEvent(xbpe.display, wid, False, PointerMotionMask|ButtonMotionMask|MotionNotify, (XEvent *)&xbpe);
XFlush(OpenedDisplay);
XCloseDisplay(OpenedDisplay);
我建议浏览xdotool的源代码。它没有专门涵盖您的问题,但为您提供了有关填充 X*Event 字段的足够信息。