使用 Python for Linux 模拟按键事件
我正在编写一个脚本来自动运行特定模型。当模型失败时,它会等待用户输入(Enter 键)。我可以检测到模型何时失败,但我无法使用 python(在 Linux 上)来模拟按键事件。 Windows 有 SendKeys 库来执行此操作,但我想知道 linux 上是否有类似的 python 库。
谢谢!
I am writing a script to automate running a particular model. When the model fails, it waits for a user input (Enter key). I can detect when the model has failed, but I am not able to use python (on linux) to simulate a key press event. Windows has the SendKeys library to do this but I was wondering if there is a similar library for python on linux.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看看这个https://github.com/SavinaRoja/PyUserInput
它在Python中对鼠标和键盘的跨平台控制
键盘控制适用于X11(linux)和Windows系统。但没有 mac 支持(当我写这个答案时)。
Have a look at this https://github.com/SavinaRoja/PyUserInput
its cross-platform control for mouse and keyboard in python
Keyboard control works on X11(linux) and Windows systems. But no mac support(when i wrote this answer).
更底层的方法是创建一个 uinput 设备,然后您可以将输入事件注入到 Linux 输入子系统中。考虑以下库:
使用后者发送
的示例:A more low-level approach would be to create an
uinput
device from which you would then inject input events into the linux input subsystem. Consider the following libraries:Example of sending
<enter>
with the latter:如果“模型”以图形方式运行(使用 X 窗口系统),则已建议的 xsendkey 是一种可能性,或者 xsendkeycode。如果它以文本方式运行(在终端窗口中),则为 pexpect。
If the "model" is running graphically (with the X window system), the already-suggested xsendkey is a possibility, or xsendkeycode. If it's running textually (in a terminal window), then pexpect.
我推荐 PyAutoGui。它使用起来非常简单,它是跨平台的,并且适用于 Python 3 和 2。
在链接页面中列出了依赖项和一些代码示例。
I recommend PyAutoGui. It's ridiculously simple to use, it's cross-platform and it's for Python 3 and 2.
In the linked page are listed the dependences and some code examples.
http://people.csail.mit.edu/anovan/hacks/xsendkey。 html
http://people.csail.mit.edu/adonovan/hacks/xsendkey.html
正如我在这个和另一个排名很高的SO响应中找到的许多解决方案一样> 已弃用(PyUserInput)或使用 evdev,失败了(
UInputError: "/dev/uinput" 无法打开用于写入
),这对我使用 Linux 来说是最简单的解决方案是 pynput。直接来自他们文档的一个例子:它就像一个魅力!
As many of the solutions I have found in this and in another well ranked SO response were either deprecated (PyUserInput) or using evdev, which failed (
UInputError: "/dev/uinput" cannot be opened for writing
) the simplest solution for me using Linux was pynput. One example directly from their docs:It worked like a charm!