在一些全局击键中,如何用Python或C++将文本粘贴到当前的活动应用程序中

2024-04-25 13:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个应用程序,它将像一个守护进程一样工作,在一些全局按键上粘贴一些文本到当前活动的应用程序(文本编辑器、浏览器、jabber客户端),我想我需要使用一些低级的xserverapi。如何用Python或C++来实现?在


Tags: 文本应用程序客户端进程粘贴浏览器全局按键
2条回答

可能你想破解xmon


虽然没有简单的方法来钩住X协议。您将需要执行“深度数据包检查”,这在应用程序事件循环中相当容易,但并非如您所愿“像守护程序”或“全局击键”一样简单。在

所以,我知道这确实是暴力和无知,但我认为您必须包装X服务器,方法是在非标准端口上启动它或发布一个环境变量,就像您使用SSH隧道来转发X服务器连接一样。在

有一个名为Xmon的X协议监视器,它的源是可用的。这可能是一个好的起点。在

我想你可以用xmacroplay utility from xmacro在X窗口下这样做。或者直接使用它——使用subprocess模块将它的命令发送到标准输入,或者阅读源代码并找出它是如何做到的!我不认为它有python绑定。在

从xmacroplay网站

xmacroplay:
Reads lines from the standard input. It can understand the following lines:

Delay [sec]     - delays the program with [sec] secundums
ButtonPress [n] - sends a ButtonPress event with button [n]
          this emulates the pressing of the mouse button [n]
ButtonRelease [n]   - sends a ButtonRelease event with button [n]
          this emulates the releasing of the mouse button [n]
... snip lots more ...

这可能是您感兴趣的命令

^{pr2}$

还有一个Xnee也做了类似的事情。在

相关问题 更多 >