Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - How to pass a keystroke (ALT+TAB) using Popen.communicate (on Linux)?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...