I know that it's possible to send printable input to subprocess
es by write
ing 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?
Popen do we need to close the connection or subprocess automatically closes the connection? Usually, the examples in the official documentation are complete. There the connection is not closed. So you do not need to close most probably.
Returned value If successful, popen() returns a pointer to an open stream that can be used to read or write to a pipe. If unsuccessful, popen() returns a NULL pointer and sets errno to one of the following values: Error Code.
With the start/end of word shortcut (sometimes called Cursor Word Start/End), you can quickly navigate one word at a time left/right on a line without holding arrow keys or having to use your mouse.
Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With