I'm writing a small CLI utility in python for a test here. It's an infinite while loop that keeps an input command running.
Every time a command is inputed, it is added to the commands list, which is initialized empty.
Here's a sample snippet:
commands = []
while True:
text = input('Utility > ')
commands.append(text)
How could I watch keypress events whilst in an input command, so if one presses arrow up or arrow down, one could shift though the already inputted commands?
input()prompt? You may get the behavior you want automatically, depending on the environment your script is running in. (There's no way to implement this yourself withinput(), you'd have to use some other method of user interaction such as thecursesmodule.)msvcrtmodule to read all keystrokes. In particular seekbhit()andgetch().