pynput stop key echo in bash
Optional descriptions
- optionally block propagation of keyboard events
- python block key events
- python listen to keyboards and stop the event message
Code talk first
set suppress=True will swallow. ALL keypresses
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| from pynput.keyboard import Key, Listener
def on_press(key): print('{0} pressed'.format( key)) if key == Key.esc: return False
with Listener( on_press=on_press, suppress=True) as listener: listener.join()
|
Issue
When use pynput to capture keyboard input like this:
1 2 3
| with Listener( on_press=on_press) as listener: listener.join()
|
the terminal will auto echo the key info, even special echo for up/down , such as:
1 2 3 4 5 6 7 8 9 10 11
| $ python3 key_listener.py Key.up pressed ^[[AKey.right pressed ^[[CKey.left pressed ^[[DKey.down pressed ^[[BKey.right pressed ^[[CKey.left pressed ^[[DKey.up pressed ^[[A'a' pressed a's' pressed
|
Now the world is slient and clean.
NOTE
but if you forget to exit the app, other apps will never get a valid key input forever.
You can also just read input from stdin,use get_char to do it