jquast / blessed

Blessed is an easy, practical library for making python terminal apps

Home Page:http://pypi.python.org/pypi/blessed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do i detect Control key sequences?

Vaxeral opened this issue · comments

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

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

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

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:

  • Ctrl + I: '\t'
  • Ctrl + J: '\n'
  • Ctrl + H: '\x08'
  • Ctrl + M: '\n'

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

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

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