发送X11 click事件不能与一些窗口工作 [英] Sending X11 click event doesn't work with some windows

查看:110
本文介绍了发送X11 click事件不能与一些窗口工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code的片断工作的大部分时间,除了在某些窗口。例如,最新的Ubuntu下它不会在文件浏览器中选择文件夹的工作。它似乎工作只是在其他地方,但这种差距是显著。我怀疑它与我如何使用XQueryPointer做的,但我已经试过几乎所有的例子我能找到。如果我使用电脑的鼠标,相反,它的工作的罚款。
FYI:我已经尝试了这些问题的答案:
发送纲领性事件
<一href=\"http://stackoverflow.com/questions/2607010/linux-how-to-capture-screen-and-simulate-mouse-movements\">Capuring鼠标输入
但他们不工作有什么不同...

这里的code:

 的#include&LT;&string.h中GT;
#包括LT&; X11 / Xlib.h&GT;
#包括LT&; X11 / Xutil.h&GT;无效SendClick(INT按钮,诠释下){
    显示*显示= XOpenDisplay(NULL);
    XEvent事件;    如果(显示屏== NULL)
    {
        返回;
    }    memset的(安培;事件,0,sizeof的(事件));    event.xbutton.button =按钮;
    event.xbutton.same_screen = TRUE;
    event.xbutton.subwindow = DefaultRootWindow(显示);    而(event.xbutton.subwindow)
    {
      event.xbutton.window = event.xbutton.subwindow;
      XQueryPointer(显示,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);
    }    event.type =下来?按钮preSS:ButtonRelease;    XSendEvent(显示,PointerWindow,真实,向下按钮pressMask:ButtonReleaseMask,&安培;事件);    XFlush(显示);    XCloseDisplay(显示);
}


解决方案

由于ninjalj以上的评论对把我在正确的轨道上。我不喜欢依靠延长做到这一点,它创建额外的依赖关系的想法,但它是一个pretty标准扩展了。完美的作品...

对于运行到同一个问题,因为我,以下code块替换我用前和code效果很好:

 的#include&LT; X11 /扩展/ XTest.h&GT;无效SendClick(INT按钮,布尔向下){
    显示*显示= XOpenDisplay(NULL);
    XTestFakeButtonEvent(显示器,按钮下来,CURRENTTIME);
    XFlush(显示);
    XCloseDisplay(显示);
}

更短!

有关Ubuntu的,不要忘了安装libxtst-dev软件包。一定要-lXtst添加到您的LDFLAGS。

The following snippet of code works most of the time, except in certain windows. For instance, under the latest Ubuntu it does not work for selecting folders in the file explorer. It seems to work just about everywhere else, but this gap is significant. I suspect it has to do with how I am using XQueryPointer, but I've tried nearly every example I can find. If I use the computer's mouse instead, it work's fine. FYI: I've already tried the answers to these questions: Sending Programmatic events Capuring Mouse Input but they don't work any different...

Here's the code:

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

void SendClick(int button, int down) {
    Display *display = XOpenDisplay(NULL);
    XEvent event;

    if(display == NULL)
    {
        return;
    }

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

    event.xbutton.button = button;
    event.xbutton.same_screen = True;
    event.xbutton.subwindow = DefaultRootWindow (display);

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

    event.type = down ? ButtonPress : ButtonRelease;

    XSendEvent(display, PointerWindow, True, down ? ButtonPressMask : ButtonReleaseMask, &event); 

    XFlush(display);

    XCloseDisplay(display);
}

解决方案

Thanks to ninjalj's comment above for putting me on the right track. I don't like the idea of relying on an extension to do this and the extra dependency it creates, but it is a pretty standard extension too. Works perfect...

For those running into the same issue as me, the following code block replaces the code I was using before and works well:

#include <X11/extensions/XTest.h>

void SendClick(int button, Bool down) {
    Display *display = XOpenDisplay(NULL);
    XTestFakeButtonEvent(display, button, down, CurrentTime);
    XFlush(display);
    XCloseDisplay(display);
}

Much shorter!

For Ubuntu, don't forget to install the libxtst-dev package. Be sure to add -lXtst to your LDFLAGS.

这篇关于发送X11 click事件不能与一些窗口工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆