6

我知道可以通过ing 向subprocesses发送可打印的输入writestdin

from subprocess import, Popen, PIPE
proc = Popen([command, goes, here], stdin=PIPE)
proc.stdin.write("m")

我将如何发送输入,例如箭头键按下、空格、返回或退格?

4

2 回答 2

9

I found someone who was trying to solve the opposite problem, create a program that could recognize the arrow keys: Recognizing arrow keys with stdin

I also found http://compgroups.net/comp.unix.programmer/how-to-send-up-arrow-key-to-popen-child/537480 which says:

"\x1B[A" for up
"\x1B[B" for down

So if \x1B is the escape character than you just append [A for up, [B for down, [C for right and [D for left and so on.

Take a look at http://en.wikipedia.org/wiki/ANSI_escape_sequences for a list of the different codes.

于 2012-10-06T02:09:52.780 回答
0

尝试pyautogui库。例如:

pyautogui.hotkey('ctrl', 'c')  # ctrl-c to copy
pyautogui.press('enter')  # press the Enter key

图书馆网页中的更多示例。对我来说它有效 - 见这里

于 2018-01-26T14:47:55.633 回答