HOWTO · Python
How to Detect Keypress in Python
Learn how to detect keyboard input in Python with keyboard, pynput, and readchar, including press and release events and single-key terminal input.
On this page
To detect keyboard input in Python, choose a library based on where input comes from: keyboard offers system-level hooks, pynput provides press and release callbacks, and readchar reads one key in a command-line program without waiting for Enter. Install the package you choose, then adapt its callback or read loop to your task. The package-specific examples below depend on third-party libraries and access to a keyboard or interactive terminal, so they are shown as API guidance and have no captured output.
Detect KeyPress Using the keyboard Module in Python
The keyboard package can install a hook that receives keyboard events. Install it in the same Python environment that runs your program with python -m pip install keyboard. On some operating systems, global keyboard hooks may require elevated privileges or may be restricted by the desktop environment. A hook receives both key-down and key-up events; filter on event.event_type == "down" when the task is to report presses. A typical integration registers a callback with keyboard.hook(on_key_event) and keeps listening with keyboard.wait("esc"). Remove a hook with keyboard.unhook_all() when listening ends but the process continues doing other work. Keep callbacks short so event handling does not delay the rest of the program.
This self-contained example uses sample event records to show the filtering logic used by a hook callback. It does not read physical keyboard events; use the package integration above for that.
events = [
{"name": "a", "event_type": "down"},
{"name": "a", "event_type": "up"},
]
for event in events:
if event["event_type"] == "down":
print(f"Key {event['name']} pressed")
Key a pressed
Detect KeyPress Using the pynput Module in Python
Use pynput when the program needs separate handlers for key presses and releases. Install it with python -m pip install pynput. A listener is created with keyboard.Listener(on_press=on_press, on_release=on_release) and started with its context manager or start(). In the press callback, character keys expose a char value, while special keys such as Escape may not; check that the attribute exists and is not None before reading it. The release callback can compare the key with keyboard.Key.esc and return False to stop the listener. Call join() when the main thread should wait for the listener to finish. This package-specific callback flow is unexecuted here because pynput is not available in the supplied runtime; operating-system permissions and desktop-session restrictions can also affect system input access.
Detect KeyPress Using the readchar Module in Python
For a command-line prompt that needs one key at a time, readchar reads a key without requiring the user to press Enter. Install it with python -m pip install readchar. In a loop, call readchar.readkey() to wait synchronously for terminal input, then compare the returned key with "q" to end the loop. Print other returned keys as they arrive. This is suited to terminal interaction, not a global hook that watches keys while another application has focus. Arrow keys and other special keys may be represented by multi-character sequences, so compare against the values documented for the terminal and library when handling them. This package-specific interaction is unexecuted here because readchar is not available in the supplied runtime.
Conclusion
Choose keyboard for a hook that reacts to system keyboard events, pynput when press and release callbacks or explicit listener lifecycle control fit the program, and readchar for a simple single-key command-line prompt. The first two listen for events beyond a prompt and can depend on operating-system permissions. readchar reads from the active terminal and does not provide global monitoring. Install the selected package in the environment that runs your script, and choose a clear stop condition such as Escape or q so the listener or read loop can finish predictably.