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

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

问题描述

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

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

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

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

谢谢米哈尔

解决方案

您可能无法完全按照您的问题说-使用Popen传递击键.您可以将字节发送到已打开的进程的stdin,但是几乎可以肯定没有在其中寻找它们.键盘事件与stdin上的数据不同.

pyautogui库库可能对此有用.安装完成后,您可以按完成的Popen启动查看器,然后使用

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

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

>

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?

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

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

Thanks, Michal

解决方案

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.

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

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天全站免登陆