Python Blessed Library Missing Key Press Events

482 views Asked by At

The blessed Python library looks great for making console apps, but I'm having trouble with the keyboard functionality. In the code below, only every second keypress is detected. Why is this please?

I'm on Windows 10 with Python 3.8.

from blessed import Terminal


term = Terminal()
print(f"{term.home}{term.black_on_skyblue}{term.clear}")

    
with term.cbreak(), term.hidden_cursor():
    while term.inkey() != 'q':
        inp = term.inkey()
        if inp.name == "KEY_LEFT":
            print("You pressed left")

1

There are 1 answers

1
nordmanden On

Move term.inkey() outside your inner while loop instead so it listens for keys first:

from blessed import Terminal
      
term = Terminal()
print(f"{term.home}{term.black_on_skyblue}{term.clear}")
    
        
with term.cbreak(), term.hidden_cursor():
    inp = term.inkey()
    while term.inkey() != 'q':
        if inp.name == "KEY_LEFT":
            print("You pressed left")