question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read keys persist there actions to the terminal

See original GitHub issue

(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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
bopprehcommented, 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/

1reaction
bopprehcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using keyboard module persists key actions to the terminal
I have been able to temporarily resolve the issue by using the suppress=True keyword argument for the read_key() function.
Read more >
Advanced Terminal Usage in Visual Studio Code
The terminal supports two different types of persistent sessions: ... Ensure the toggle sidebar visibility keybinding skips the shell "workbench.action.
Read more >
Home and End keys not working - ArchWiki
A frequent problem in command line programs is that keys like Home and End do not work as expected. This is usually because...
Read more >
Windows Terminal Actions | Microsoft Learn
Learn how to create custom actions for Windows Terminal. ... If the action does not have keys, it will appear in the command...
Read more >
micro/keybindings.md at master · zyedidia/micro - GitHub
In iTerm2, you can do this in Preferences->Profiles->Keys then click the + , input your keybinding, and for the Action select Send Escape...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found