Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get interactive input from user and to be able to use arrowkeys while entering input?

Tags:

python

When I do a raw_input() and enter values, I am not able to use my arrow-keys to change stuff... is there any way for doing that?

Thanx readline module helps in line editing features. How to use the readline module? Just importing the readline module works!

like image 466
Sriram Avatar asked Nov 10 '09 10:11

Sriram


3 Answers

Try loading the readline module (import readline). That will make things work for you.

like image 192
Andre85 Avatar answered Nov 15 '22 20:11

Andre85


That's not how raw_input() works. It reads a line from the prompt, and then processes it after the newline character.

The docs are pretty clear: http://docs.python.org/library/functions.html#raw_input

If you try to throw arrow keys into the mix, your terminal is likely to add those characters to the returned string. Then again, it may not, depending on your operating system. Don't count on it.

Perhaps you want the readline module?

http://docs.python.org/library/readline.html#module-readline

like image 33
Paul McMillan Avatar answered Nov 15 '22 20:11

Paul McMillan


For those looking for examples, there's a nice introduction to the readline module here: https://pymotw.com/2/readline/

like image 31
user3139585 Avatar answered Nov 15 '22 22:11

user3139585