发送击键到X窗口
Sending Keystrokes to a X Window
我目前正在尝试使用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
,它的行为会有所不同。
直接来自xdotool手册:
<SENDEVENT笔记/strong>
如果您试图将键输入发送到特定的窗口,并且它确实这样做了看起来不工作,那么很可能您的应用程序正在忽略xdotool正在生成的事件。这是相当常见的。
将击键发送到特定窗口使用的API与只需键入活动窗口。如果指定"xdotool type"xdotool将生成关键事件并发送它们直接到12345窗口。但是,X11服务器将设置一个特殊的标志以这种方式生成的所有事件(参见XEvent.xany)。send_event在X11手册)。
需要注意的是,对于键和鼠标事件,我们只使用当一个特定的窗口成为目标时。否则,我们使用XTEST.
一些程序可以配置为接受事件,即使它们是由xdotool生成。寻找你的申请文件帮助。
具体的应用说明(来自作者的测试)当它没有焦点时,似乎忽略了所有输入。* xterm可以是在运行时使用ctrl+左键单击"允许SendEvents"*进行配置gnome终端默认接受生成的输入。
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 用于窗口的 HID 终端
- SFML 文本未绘制在窗口上
- 如何为窗口截屏
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息