发送击键到X窗口

Sending Keystrokes to a X Window

本文关键字:窗口      更新时间:2023-10-16

我目前正在尝试使用xdotool将密钥发送给进程(我理解它可能不适用于所有未设置_NET_WM_PID的进程)。我无法将按键从焦点发送到其他窗口。如果您向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;
}

除了测试xdotool的功能外,我还查看了xdotool的源代码。有趣的是,我发现他们使用Xtest将击键发送到聚焦窗口(CURRENTWINDOW), X11的XSendEvent发送到其他窗口。我转向xdotool,因为我不能让XSendEvent工作,Xtest不能向任何其他窗口发送键,而不是聚焦窗口。

我没有正确使用xdotool吗?xdotool不能与X11的所有*nix操作系统一起工作吗?

[我在Ubuntu 13.04上运行这个]


编辑

所以,它看起来是工作的,但不是对它找到的所有窗口。例如,它适用于firefox,但不适用gedit和gnome-terminal,尽管它可以通过其pid找到gedit和gnome-terminal。如果我使用CURRENTWINDOW,它的行为会有所不同。

所以,如果有人能解释一下为什么会这样,那就太好了。比如,它与XEvent中的强制发送标志有关吗?

直接来自xdotool手册:

<

SENDEVENT笔记/strong>

如果您试图将键输入发送到特定的窗口,并且它确实这样做了看起来不工作,那么很可能您的应用程序正在忽略xdotool正在生成的事件。这是相当常见的。

将击键发送到特定窗口使用的API与只需键入活动窗口。如果指定"xdotool type"xdotool将生成关键事件并发送它们直接到12345窗口。但是,X11服务器将设置一个特殊的标志以这种方式生成的所有事件(参见XEvent.xany)。send_event在X11手册)。

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

一些程序可以配置为接受事件,即使它们是由xdotool生成。寻找你的申请文件帮助。

具体的应用说明(来自作者的测试)当它没有焦点时,似乎忽略了所有输入。* xterm可以是在运行时使用ctrl+左键单击"允许SendEvents"*进行配置gnome终端默认接受生成的输入。