问题描述
我有两个 python 脚本.第一个只是等待用户键盘输入的脚本.当用户按下一个键时,它会打印一个按下的键值.
I have two python scripts. First one is just a script waiting for user keyboard input. When user presses a key it prints a pressed key value.
第二个脚本像这样使用 Popen 通过子进程调用第一个脚本
Second script calls first one through subprocess using Popen like this
p = Popen('python first_script.py', shell=True, universal_newlines=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
print p.communicate(input="some value paased through")[0]
当我通过字符串值发送时,我得到了它.但我不知道如何发送键盘事件以及如何正确读取它.
I got it working when I send through string values. But I don't know how to send keyboard event and how to read it properly.
推荐答案
subprocess
本身没有发送键盘事件"(到子进程或任何其他进程)的设施.您需要其他方法,例如 这篇文章 展示的适用于 Windows 的方法.
subprocess
per se has no facilities to "send keyboard events" (to the sub-process or to any other process). You need other aproaches, such as the one this article shows for Windows.
这篇关于使用子进程发送键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!