使用 Python for Linux 模拟按键事件

发布于 2024-08-27 07:08:33 字数 165 浏览 13 评论 0原文

我正在编写一个脚本来自动运行特定模型。当模型失败时,它会等待用户输入(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南城追梦 2024-09-03 07:08:33

看看这个https://github.com/SavinaRoja/PyUserInput
它在Python中对鼠标和键盘的跨平台控制

键盘控制适用于X11(linux)和Windows系统。但没有 mac 支持(当我写这个答案时)。

from pykeyboard import PyKeyboard
k = PyKeyboard()

# To Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)

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).

from pykeyboard import PyKeyboard
k = PyKeyboard()

# To Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)
始终不够 2024-09-03 07:08:33

更底层的方法是创建一个 uinput 设备,然后您可以将输入事件注入到 Linux 输入子系统中。考虑以下库:

使用后者发送 的示例:

from evdev import uinput, ecodes as e

with uinput.UInput() as ui:
     ui.write(e.EV_KEY, e.KEY_ENTER, 1)
     ui.write(e.EV_KEY, e.KEY_ENTER, 0)
     ui.syn()

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:

from evdev import uinput, ecodes as e

with uinput.UInput() as ui:
     ui.write(e.EV_KEY, e.KEY_ENTER, 1)
     ui.write(e.EV_KEY, e.KEY_ENTER, 0)
     ui.syn()
十年不长 2024-09-03 07:08:33

如果“模型”以图形方式运行(使用 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.

携余温的黄昏 2024-09-03 07:08:33

我推荐 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.

云醉月微眠 2024-09-03 07:08:33

正如我在这个和另一个排名很高的SO响应中找到的许多解决方案一样> 已弃用(PyUserInput)或使用 evdev,失败了(UInputError: "/dev/uinput" 无法打开用于写入),这对我使用 Linux 来说是最简单的解决方案是 pynput。直接来自他们文档的一个例子:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

它就像一个魅力!

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:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

It worked like a charm!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文