jquast / blessed

Blessed is an easy, practical library for making python terminal apps
http://pypi.python.org/pypi/blessed
MIT License
1.18k stars 71 forks source link

Arrow key input not working #246

Closed tobyMickelson closed 1 year ago

tobyMickelson commented 1 year ago

When using with term.cbrake():, I am unable to get usable input. It starts by not accepting input then thinks every input is KEY_LEFT.

Here is what I tested arrow key input with:

from blessed import Terminal

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

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

I also tested it with term.raw()

System information: MacOS Monterey 12.6.1 (21G217) Python 3.10.5 (v3.10.5:f377153967, Jun 6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

jquast commented 1 year ago

try this instead,

from blessed import Terminal

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

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

the inp variable was never re-assigned after the first key is pressed, so it was always displaying the first key that was pressed

tobyMickelson commented 1 year ago

Thanks for the help, but now it only works with raw but not cbrake.

Traceback (most recent call last):
  File "/Users/Username/Documents/Python/test.py", line 7, in <module>
    with term.cbrake():
AttributeError: __enter__

shell returned 1

Press ENTER or type command to continue

BTW, this is also a school issued MacBook, and that might be causing some of the problems.

avylove commented 1 year ago

Try term.cbreak(). There was a typo in the example.

tobyMickelson commented 1 year ago

Thank you for helping fix my stupid typo.