Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Keyboard module in Python
The keyboard module in Python allows you to control and monitor keyboard events programmatically. It provides functionality for simulating keystrokes, detecting key presses, and creating keyboard shortcuts across different platforms.
Installation
Install the keyboard module using pip ?
pip install keyboard
Note: This module requires administrator/root privileges to work properly on most systems.
Key Features
- Cross-platform compatibility (Windows, macOS, Linux)
- Simulate keyboard input and key combinations
- Block or intercept specific key actions
- Create global hotkeys and keyboard shortcuts
- Record and replay keyboard events
Writing Text and Key Combinations
Use keyboard.write() to simulate typing and keyboard.press_and_release() for key combinations ?
import keyboard
import time
# Write text automatically
keyboard.write("Hello TutorialsPoint!")
# Simulate key combinations
keyboard.press_and_release('ctrl+a') # Select all
keyboard.press_and_release('ctrl+c') # Copy
# Press individual keys
keyboard.press_and_release('enter')
keyboard.press_and_release('tab')
Waiting for Key Events
The keyboard.wait() method blocks execution until a specific key is pressed ?
import keyboard
print("Type some text, then press 'esc' to exit...")
keyboard.wait('esc')
print("Program terminated!")
# Wait for key combinations
print("Press Ctrl+Q to quit...")
keyboard.wait('ctrl+q')
Creating Hotkeys
Register callback functions that execute when specific key combinations are pressed ?
import keyboard
def on_hotkey():
print("Hotkey activated!")
def save_file():
print("File saved!")
# Register hotkeys
keyboard.add_hotkey('ctrl+shift+h', on_hotkey)
keyboard.add_hotkey('ctrl+s', save_file)
print("Hotkeys registered. Press Ctrl+Shift+H or Ctrl+S")
keyboard.wait('esc') # Keep program running until ESC
Recording and Replaying Events
Record keyboard activity and replay it later ?
import keyboard
print("Recording started. Press 'esc' to stop...")
recorded = keyboard.record(until='esc')
print("Recording stopped. Press 'space' to replay...")
keyboard.wait('space')
print("Replaying recorded events...")
keyboard.play(recorded, speed_factor=1)
Common Methods
| Method | Description | Example |
|---|---|---|
write(text) |
Types the specified text | keyboard.write("Hello") |
press_and_release(key) |
Presses and releases a key | keyboard.press_and_release('enter') |
wait(key) |
Waits for a key press | keyboard.wait('esc') |
add_hotkey(combo, callback) |
Registers a hotkey | keyboard.add_hotkey('ctrl+c', func) |
Important Considerations
- Permissions: Requires administrator privileges on most systems
- Security: Use responsibly as it can simulate user input
- Platform differences: Key names may vary between operating systems
- Testing: Test thoroughly before deploying keyboard automation scripts
Conclusion
The keyboard module provides powerful functionality for automating keyboard input and creating interactive applications. Use it responsibly with proper permissions for tasks like automation scripts, accessibility tools, and custom keyboard shortcuts.
