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

How do i detect Control key sequences? #222

Closed Vaxeral closed 2 months ago

Vaxeral commented 2 years ago

Im trying to detect ctrl+c or other variants in my vscode terminal. Ctrl sequences seem to not appear when using inkey.

avylove commented 2 years ago

Are you using using cbreak or raw mode? You'll need raw mode in order to capture some control sequences.

Vaxeral commented 2 years ago

im in raw mode. I saw your other issues and i found that i could use the sequence \x01 for control c for example.

Minabsapi commented 1 year ago

Same thing for me. Variant of the "rich event loop" snippet for reference:

#!/bin/python3

from blessed import Terminal

term = Terminal()

print(f"{term.home}{term.black_on_skyblue}{term.clear}")
print("press 'q' to quit.")
with term.raw():
    val = ''
    while val.lower() != 'q':
        val = term.inkey(timeout=3)
        if not val:
           print("It sure is quiet in here ...")
        elif val.is_sequence:
           print("got sequence: {0}.".format((str(val), val.name, val.code)))
        elif val:
           print(f"got {val}. ({val.code})")
    print(f'bye!{term.normal}')

When launching it from Bash, almost all Keystroke object on Ctrl modifier sequences have a code None. Some exceptions:

Edit: tried with keymatrix.py, and the code seems to be reported there

avylove commented 1 year ago

What's an example of a key you're seeing in keymatrix that you aren't seeing in the above code?

jquast commented 1 year ago

Hello @Minabsapi,

Try changing,

           print(f"got {val}. ({val.code})")

to,

           print(f"got {val!r}. ({val.code})")

that is, display repr(val) rather than str(val) -- as the val in this case is the raw control character. When written to stdout, most terminals do not display anything

jquast commented 2 months ago

Closing as duplicate of #58