Using Python 2.7, I'd like my program to accept Keyboard Arrow Keys – e.g. ↑ while inputting into MacOS Terminal.
Pressing ↑ in Terminal outputs ^[[A
in it, so I've assumed this is the escape key sequence.
However, pressing the ↑ and RETURN at the raw_input()
prompt doesn't seem to produce a string that can then be conditioned:
string = raw_input('Press ↑ Key: ')
if string == '^[[A':
print '↑' # This doesn't happen.
How can this be done?
Note that I'm not trying to input whatever the previous line was in the shell (I think this is was import readline
manages). I just want to detect that an arrow key on the keyboard was entered somehow.