Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do i detect Control key sequences? #222

Closed
Vaxeral opened this issue Oct 10, 2021 · 6 comments
Closed

How do i detect Control key sequences? #222

Vaxeral opened this issue Oct 10, 2021 · 6 comments

Comments

@Vaxeral
Copy link

Vaxeral commented Oct 10, 2021

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

@avylove
Copy link
Collaborator

avylove commented Oct 11, 2021

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

@Vaxeral
Copy link
Author

Vaxeral commented Oct 12, 2021

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
Copy link

Minabsapi commented Oct 16, 2022

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

@avylove
Copy link
Collaborator

avylove commented Oct 17, 2022

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

@jquast
Copy link
Owner

jquast commented Oct 17, 2022

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
Copy link
Owner

jquast commented Jun 26, 2024

Closing as duplicate of #58

@jquast jquast closed this as completed Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants