1

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?

3
  • 2
    Have you actually tried pressing the up arrow while at an 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 with input(), you'd have to use some other method of user interaction such as the curses module.) Commented Feb 16, 2021 at 17:43
  • Nice. I'll check curses. Thanks for pointing a direction. Commented Feb 16, 2021 at 17:50
  • On Windows you can use the msvcrt module to read all keystrokes. In particular see kbhit() and getch(). Commented Feb 16, 2021 at 18:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.