如何使用 Popen.communicate(在 Linux 上)传递击键(ALT+TAB)? [英] How to pass a keystroke (ALT+TAB) using Popen.communicate (on Linux)?

查看:21
本文介绍了如何使用 Popen.communicate(在 Linux 上)传递击键(ALT+TAB)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Raspberry Pi 上以全屏模式打开了两个图像(使用 ristretto 图像查看器).当满足某些条件时,我需要传递一个 ALT+TAB 击键组合来显示另一个.我正在尝试使用 Popen.communicate,但我不知道如何传递诸如 ALT+TAB 之类的组合键).有没有人有任何想法?

I have two images open in a full-screen mode on my Raspberry Pi (using ristretto image viewer). When certain conditions are met, I need to pass an ALT+TAB keystroke combination to display the other one. I am trying to use Popen.communicate, but I don't know how to pass a key combination like ALT+TAB). Does anyone have any ideas?

我需要的是下面的内容(用工作代码替换ALT+TAB"):

What I need is something like below (replacing "ALT+TAB" with a working code):

s = Popen(['ristretto', '-f', 'my.gif' ,'&'],stdin=PIPE)
if my_condition:
    s.communicate("ALT+TAB")

我听说过 SendKeys 包,但它仅适用于 Windows...

I heard about SendKeys package, but it works only for Windows...

谢谢,米哈尔

推荐答案

您可能无法完全按照您的问题所说的去做 - 使用 Popen 传递击键.您可以将字节发送到您打开的进程的 stdin,但它几乎肯定不会在那里寻找它们.键盘事件与来自 stdin 的数据不同.

You probably can't do exactly what your question says - pass keystrokes using Popen. You can send bytes to the stdin of the process you've opened, but it's almost certainly not looking there for them. Keyboard events are different from data coming on stdin.

pyautogui 库 库可能对此有用.安装完成后,您可以使用 Popen 启动查看器,就像您所做的那样,然后使用

The pyautogui library library could be useful for this purpose, however. Once it's installed, you could launch your viewer with Popen, as you've done, and then use

pyautogui.hotkey('alt', 'tab')

将 alt+tab 发送到前台应用程序.您可能需要添加一个短暂的延迟,以确保 ristretto 在发送密钥之前已完成启动.有关如何使用它的更多详细信息,请参阅 pyautogui 的键盘文档.

To send alt+tab to the foreground application. You may need to add a short delay to make sure ristretto has finished launching before sending the keys. See pyautogui's keyboard documentation for more details about how to use it.

这篇关于如何使用 Popen.communicate(在 Linux 上)传递击键(ALT+TAB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆