Inaimathi
Inaimathi

Reputation: 14065

Sending Arrow Keys to Popen

I know that it's possible to send printable input to subprocesses by writeing to their stdin

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

How would I go about sending input such as arrow key presses, space, return, or backspace?

Upvotes: 7

Views: 8274

Answers (2)

michalrudko
michalrudko

Reputation: 1530

Try pyautogui library. For example:

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

More examples in the library webpage. For me it worked - see here.

Upvotes: -1

Nathan Villaescusa
Nathan Villaescusa

Reputation: 17629

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.

Upvotes: 9

Related Questions