Keyboard: Global Keyboard Events and Simulation

0.13.5 · active · verified Sun Apr 12

The `keyboard` library for Python provides full control over the keyboard, allowing users to hook global events, register hotkeys, simulate key presses, and much more. It works on Windows and Linux, with experimental support for macOS. The current version is 0.13.5, and it maintains an active development and release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to simulate key presses, type text, and register complex hotkeys using the `keyboard` library. The program will wait until 'esc' is pressed to terminate.

import keyboard
import time

# Simulate pressing Shift+S then Space
keyboard.press_and_release('shift+s, space')

# Type a sentence
keyboard.write('The quick brown fox jumps over the lazy dog.')

# Register a hotkey (Ctrl+Shift+A) to print a message
keyboard.add_hotkey('ctrl+shift+a', print, args=('Ctrl+Shift+A triggered',))

# Register a hotkey for a sequence (Page Up then Page Down) to type text
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

print("Press Ctrl+Shift+A or Page Up then Page Down. Press 'esc' to exit.")

# Blocks until 'esc' is pressed
keyboard.wait('esc')

view raw JSON →