Yet Another Keypress Handler

raw JSON →
0.4.1 verified Fri May 01 auth: no python

A cross-platform library for handling keypress events in the terminal. Supports both Unix and Windows systems. Current version: 0.4.1. Release cadence: irregular.

pip install python-yakh
error AttributeError: module 'yakh' has no attribute 'KeypressHandler'
cause Probably installed the wrong package or imported incorrectly. The package is 'python-yakh' but import is 'yakh'.
fix
Ensure you installed 'python-yakh' and import as 'from yakh import KeypressHandler'.
error OSError: [Errno 25] Inappropriate ioctl for device
cause Occurs on some Unix systems when the terminal is not a TTY (e.g., running in a pipe or IDE terminal).
fix
Run the script in a proper terminal or check with sys.stdin.isatty() before using yakh.
gotcha The KeypressHandler runs in a separate thread. Ensure you stop the handler properly to avoid hanging processes. Always call handler.stop() before exiting.
fix Use a try/finally block or context manager to stop the handler.
gotcha On Unix, the handler sets the terminal to raw mode. If the program crashes or exits without calling stop(), the terminal may be left in a broken state. Use atexit or signal handlers to restore terminal settings.
fix Register handler.stop() with atexit.register() or use a context manager.
deprecated In version 0.2.1 and earlier, the handler had a bug where the fd was not reset to blocking state. Make sure to use 0.2.1 or later.
fix Upgrade to 0.2.1+ using pip install --upgrade python-yakh

Simple example: listen for keypresses for 5 seconds and print each key.

from yakh import KeypressHandler

def on_key(key):
    print(f"Pressed: {key}")

handler = KeypressHandler()
handler.key_pressed += on_key
handler.start()
import time
time.sleep(5)
handler.stop()