C++ 向X窗口发送击键

C++ 向X窗口发送击键,c++,ubuntu,x11,xdotool,C++,Ubuntu,X11,Xdotool,我目前正在尝试使用xdotool向进程发送密钥(我知道它可能不适用于所有未设置_NET_WM_PID的进程)。我无法将按键从焦点发送到其他windows。如果您正在向CURRENTWINDOW发送击键,则它确实有效。下面是我用来测试xdotool功能的代码片段 extern "C"{ #include <xdo.h> } //extern "C" xdo_window_search #include <iostream> #include <string.h&g

我目前正在尝试使用xdotool向进程发送密钥(我知道它可能不适用于所有未设置_NET_WM_PID的进程)。我无法将按键从焦点发送到其他windows。如果您正在向
CURRENTWINDOW
发送击键,则它确实有效。下面是我用来测试xdotool功能的代码片段

extern "C"{
  #include <xdo.h>
}
//extern "C" xdo_window_search
#include <iostream>
#include <string.h>

using namespace std;

int main(){
    xdo_t* p_xdo = xdo_new(NULL);

    // Allocate memory for search query.
    xdo_search_t s;
    // Clear the allocated memory.
    memset(&s, 0, sizeof(xdo_search_t));
    // Set the search query.
    s.pid = 1916;
    s.max_depth = -1;
    s.searchmask = SEARCH_PID;
    s.require = xdo_search::SEARCH_ANY;
    // Allocate memory for output
    Window* windows;
    int no_windows;
    xdo_window_search(p_xdo,&s,&windows,&no_windows);
    cout << no_windows << endl;
    // Prints all windows' names with matching criteria
    for( int i=0;i<no_windows;i++ ){
        unsigned char * name;
        int size;
        int type;
        xdo_get_window_name(p_xdo,windows[i],&name,&size,&type);
        cout << i << ":" << name << endl;
    }
    for( int i=0;i<no_windows;i++ ){
        xdo_type(p_xdo,windows[i],"Hello World",0);
    }
    //xdo_type(p_xdo,CURRENTWINDOW,"Hello World",0); // This does work.
    return 0;
}
extern“C”{
#包括
}
//外部“C”xdo\u窗口\u搜索
#包括
#包括
使用名称空间std;
int main(){
xdo_t*p_xdo=xdo_new(空);
//为搜索查询分配内存。
xdo搜索;
//清除分配的内存。
memset(&s,0,sizeof(xdo_搜索));
//设置搜索查询。
s、 pid=1916;
s、 最大深度=-1;
s、 searchmask=SEARCH\u PID;
s、 require=xdo\u search::search\u ANY;
//为输出分配内存
窗口*窗口;
int no_窗口;
xdo_窗口搜索(p_xdo,&s,&windows,&no_窗口);

可以直接从xdool手册中选择:

发送事件注释

如果您正试图将密钥输入发送到特定窗口,并且确实如此 如果您的应用程序似乎无法正常工作,则可能会被忽略 Xdool正在生成事件。这相当常见

将击键发送到特定窗口使用不同于的API 只需键入活动窗口。如果指定“xdotool类型” --窗口12345 hello'xdool将生成关键事件并发送它们 直接转到窗口12345。但是,X11服务器将设置一个特殊标志 以这种方式生成的所有事件(请参阅中的XEvent.xany.send_事件 X11的手册)。许多程序遵守此标志并拒绝这些事件。

重要的是要注意,对于按键和鼠标事件,我们只使用 当一个特定的窗口成为目标时,使用XSendEvent。否则,我们使用XTEST

某些程序可以配置为接受事件,即使它们是 由xdotool生成。请查阅您的应用程序文档 救命啊

具体应用说明(来自作者的测试):*Firefox3 似乎在没有焦点时忽略所有输入。*xterm可以 在使用ctrl+leftclick运行时配置“允许发送事件”* 默认情况下,gnome终端似乎接受生成的输入


只是一个评论:我发现sikuli在尝试将任意鼠标和键盘事件发送到windows时非常有用。如果你没有绑定到xdotool,你可能想尝试一下sikuli。谢谢你的建议。我没有绑定到xdotool。我已经通过Xlib和Xtest实现了一个解决方案。如果我需要发送任意消息,我会研究它鼠标事件。谢谢。@swtdrgn您能分享您的Xlib和Xtest替代解决方案吗?