1

我正在尝试在 ubuntu 12.04 中使用 xlib 发送鼠标单击事件,当我在桌面栏图标中单击时所有工作正常,当我在每个窗口的标题栏中单击时工作(关闭、最小化、最大化窗口)但是在某些窗口中单击内部不起作用,仅在我的 qt 创建器窗口中起作用,但是当我单击时,例如,主文件夹图标然后将鼠标移动到文件夹内,我无法在文件夹或菜单栏中进行任何单击, 仅适用于 windows 的标题栏。

也许是 Ubuntu Unity 桌面的错误?这是我在互联网上找到的代码:

#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseClick(int button)
{
    Display *display = XOpenDisplay(NULL);

    XEvent event;

    if(display == NULL)
    {
        fprintf(stderr, "Errore nell'apertura del Display !!!\n");
        exit(EXIT_FAILURE);
    }

    memset(&event, 0x00, sizeof(event));

    event.type = ButtonPress;
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

    event.xbutton.subwindow = event.xbutton.window;

    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);
    }

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");

    XFlush(display);

    usleep(100000);

    event.type = ButtonRelease;
    event.xbutton.state = 0x100;

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");

    XFlush(display);

    XCloseDisplay(display);
}

我的 ubuntu 在 vmware 播放器中,我使用 Xlib 的 XWarpPointer 移动光标,谢谢您的帮助。

4

1 回答 1

1

我有同样的问题,我只是使用 Xext 和 Xtest 扩展解决了它。

#include <X11/extensions/XTest.h>

int main(int argc, char ** argv)
{
    XEvent event;
    Display *dpy = XOpenDisplay (NULL);

    /* Fake the mouse button Press and Release events */
    XTestFakeButtonEvent (dpy, 1, True,  CurrentTime);
    XTestFakeButtonEvent (dpy, 1, False, CurrentTime);
    XCloseDisplay (dpy);
    return 0;
}
于 2017-03-18T09:51:46.513 回答