DEV Community

MrRobot
MrRobot

Posted on

keyboard – Global Keyboard Hook for Python

The keyboard library allows Python programs to monitor, capture, and simulate keyboard events globally. It can detect key presses, record macros, create hotkeys, and automate typing tasks across all applications. It’s widely used for automation, accessibility tools, and game or app controls.


Installation:

pip install keyboard
Enter fullscreen mode Exit fullscreen mode

Example usage:

import keyboard

# Print key name when pressed
keyboard.on_press(lambda e: print(f"Key {e.name} pressed"))

# Wait for the user to press 'esc'
keyboard.wait('esc')

# Simulate typing
keyboard.write("Hello, this was typed by Python!")
Enter fullscreen mode Exit fullscreen mode

PyPI page: https://pypi.org/project/keyboard/
GitHub page: https://github.com/boppreh/keyboard


3 Project Ideas:

  1. Create a global hotkey system to launch scripts or apps.
  2. Build a macro recorder and playback tool.
  3. Develop a typing speed tracker or keylogger for analytics (ethical use).

Top comments (0)