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

Read keys persist there actions to the terminal #482

Closed
GyBeans opened this issue Sep 6, 2021 · 5 comments
Closed

Read keys persist there actions to the terminal #482

GyBeans opened this issue Sep 6, 2021 · 5 comments

Comments

@GyBeans
Copy link

GyBeans commented Sep 6, 2021

(This may be related to issue #22)

Relating to this StackOverflow post: Using keyboard module persists key actions to the terminal

It seems that when reading keys using read_key() that (say for example a character key or the 'Enter' key is read) the action for that key will persist in the terminal later if, say, the built-in input() function is used.

E.g. Pressing the, say, q key at an initial part of the program will later pre-populate input() with the character q when the user is next require to pass input. It would be nice if the read character from read_key() did not get sent to stdin or stdout.

@boppreh
Copy link
Owner

boppreh commented Sep 6, 2021

Yes, what you are looking for is "suppression" of events. Try read_key(suppress=True), the event should not be populated in stdin.

But please keep in mind that this library is meant for global events. So the character will be captured even if it was pressed when the terminal was not focused. If you want to just capture a single key without outputting it, something like getch is more appropriate. It's a bit tricky to get working on Python, but there's a module that solves it for you: https://pypi.org/project/getch/

@GyBeans
Copy link
Author

GyBeans commented Sep 6, 2021

Ahh that is helpful, thank you!
Unfortunately for my use case, I believe that getch() would not work for me as I also need the use of special keys e.g. Enter and Backspace.

@boppreh
Copy link
Owner

boppreh commented Sep 6, 2021

If it's just backspace and enter, you are in luck, those actually count as characters and are returned by getch:

>>> import msvcrt
>>> msvcrt.getch() # Backspace
b'\x08'
>>> msvcrt.getch() # Enter
b'\r'

What you cannot get are modifiers, like alt, shift, caps-lock, etc, and F1-F12 keys (they just report 0x00).

Basically anything with its own ASCII code (and by extension present in Unicode), should work.

@GyBeans
Copy link
Author

GyBeans commented Sep 6, 2021

Amazing, thank you @boppreh!

@boppreh
Copy link
Owner

boppreh commented Sep 6, 2021

You're welcome! I'm closing this ticket for now, and including a line about getch in the readme. Good luck in your project!

@boppreh boppreh closed this as completed Sep 6, 2021
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

2 participants