鼠标按键模拟-右键和上下文菜单问题

发布于 2024-11-03 10:39:53 字数 1935 浏览 2 评论 0原文

我正在对远程触摸板进行编程。所以我需要用C++模拟Linux上的鼠标按钮按下。我使用 Xlib 函数来做到这一点。

两个触摸板按钮都几乎可以正常工作,但例如,当我在 Linux 桌面上按下右键时,会显示上下文菜单,然后无法通过左键按下模拟从中选择任何内容。我什至无法通过单击外部菜单来摆脱该上下文菜单。

有人可以帮助我如何让我的模拟触摸板按钮表现得像真实的按钮吗?

这是我的代码:

void MainWindow::mouseClick(int button)
{
    // Open X display
    Display *display = XOpenDisplay (NULL);
    if (display == NULL)
    {
        cerr << "Can't open display!" << endl;
    }

    // Create and setting up the event
    XEvent event;
    memset (&event, 0, sizeof (event));
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer (display, RootWindow(display, DefaultScreen(display)),
                     &event.xbutton.root, &event.xbutton.subwindow,
                     &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);
    }

    // Press
    event.type = ButtonPress;
    if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
    XFlush (display);
    usleep (1);

    // Release
    event.type = ButtonRelease;
    event.xbutton.state = 0x100;
    if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
    XFlush (display);
    usleep (1);

    XCloseDisplay (display);
}

I am programming remote touchpad. So I need to simulate mouse button press on Linux in C++. I use Xlib functions to do that.

Both touchpad buttons works almost fine, but for example when I press right button on the Linux desktop, context menu shows up and then it's impossible to choose anything from it by left button press simulation. I can't even get rid of that context menu by clicking outside of it.

Can you someone help me how to make my simulated touchpad buttons behave like real ones, please?

Here is my code:

void MainWindow::mouseClick(int button)
{
    // Open X display
    Display *display = XOpenDisplay (NULL);
    if (display == NULL)
    {
        cerr << "Can't open display!" << endl;
    }

    // Create and setting up the event
    XEvent event;
    memset (&event, 0, sizeof (event));
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer (display, RootWindow(display, DefaultScreen(display)),
                     &event.xbutton.root, &event.xbutton.subwindow,
                     &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);
    }

    // Press
    event.type = ButtonPress;
    if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
    XFlush (display);
    usleep (1);

    // Release
    event.type = ButtonRelease;
    event.xbutton.state = 0x100;
    if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
    XFlush (display);
    usleep (1);

    XCloseDisplay (display);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-11-10 10:39:53

我在处理 Xlib 时遇到了类似的问题。我最终使用了 Xtest 库。这很简单。只需在您的 Linux 机器上安装 libxtst 即可。

XTestFakeMotionEvent(display, -1, x, y, CurrentTime );
XTestFakeButtonEvent(display, button, True, CurrentTime); //Mouse down
XTestFakeButtonEvent(display, button, False, CurrentTime); //Mouse up

您根本不必担心 Windows 层次结构!

I faced similar problem while dealing with Xlib. I finally ended up using Xtest library. It is pretty simple. Just install libxtst on your linux box.

XTestFakeMotionEvent(display, -1, x, y, CurrentTime );
XTestFakeButtonEvent(display, button, True, CurrentTime); //Mouse down
XTestFakeButtonEvent(display, button, False, CurrentTime); //Mouse up

You need not worry about the windows hierarchy at all!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文