Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Arrow Keys to Popen

Tags:

python

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?

like image 219
Inaimathi Avatar asked Oct 06 '12 01:10

Inaimathi


People also ask

Does Popen need to be closed?

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.

What does the popen () return on success?

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.

How do I navigate without the arrow keys?

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.

What is Popen in Python?

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'.


1 Answers

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.

like image 146
Nathan Villaescusa Avatar answered Sep 21 '22 10:09

Nathan Villaescusa