Cross-platform GUI automation library that enables programmatic control of mouse, keyboard, and screen interactions.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive keyboard automation including key presses, text typing, and keyboard shortcuts with support for all standard keys, modifiers, and special characters. Provides precise control over key timing and supports both individual key operations and complex keyboard combinations.
Press and release individual keys or key combinations with configurable timing and repetition.
def press(keys, presses=1, interval=0.0, logScreenshot=None, _pause=True):
"""
Press and release key(s) one or more times.
Parameters:
- keys (str or list): Key name(s) to press. Can be single key or list of keys
- presses (int): Number of times to press the key(s) (default: 1)
- interval (float): Seconds to wait between key presses (default: 0.0)
- logScreenshot (bool): Log screenshot for debugging (default: None)
- _pause (bool): Apply global PAUSE after operation (default: True)
Returns:
None
Examples:
press('enter')
press(['ctrl', 'c'])
press('a', presses=3, interval=0.1)
"""
def keyDown(key, logScreenshot=None, _pause=True):
"""
Press key down without releasing.
Parameters:
- key (str): Key name to press down
- logScreenshot (bool): Log screenshot for debugging (default: None)
- _pause (bool): Apply global PAUSE after operation (default: True)
Returns:
None
"""
def keyUp(key, logScreenshot=None, _pause=True):
"""
Release key without pressing.
Parameters:
- key (str): Key name to release
- logScreenshot (bool): Log screenshot for debugging (default: None)
- _pause (bool): Apply global PAUSE after operation (default: True)
Returns:
None
"""Type text strings character by character with configurable timing between characters.
def typewrite(message, interval=0.0, logScreenshot=None, _pause=True):
"""
Type text message character by character.
Parameters:
- message (str): Text to type
- interval (float): Seconds to wait between each character (default: 0.0)
- logScreenshot (bool): Log screenshot for debugging (default: None)
- _pause (bool): Apply global PAUSE after operation (default: True)
Returns:
None
Note: Only types printable characters. Use press() for special keys.
"""
def write(message, interval=0.0, logScreenshot=None, _pause=True):
"""Alias for typewrite() - type text message character by character."""Execute keyboard shortcuts by pressing multiple keys in sequence and releasing them in reverse order.
def hotkey(*args, **kwargs):
"""
Perform keyboard shortcut by pressing keys in sequence, then releasing in reverse.
Parameters:
- *args: Key names to press in order (e.g., 'ctrl', 'c')
- **kwargs: Same parameters as press() (logScreenshot, _pause)
Returns:
None
Examples:
hotkey('ctrl', 'c') # Copy
hotkey('ctrl', 'shift', 's') # Save As
hotkey('alt', 'tab') # Switch windows
"""
def shortcut(*args, **kwargs):
"""Alias for hotkey() - perform keyboard shortcut."""Advanced key state control using context managers for holding keys during other operations.
def hold(keys, logScreenshot=None, _pause=True):
"""
Context manager for holding key(s) down during other operations.
Parameters:
- keys (str or list): Key name(s) to hold down
- logScreenshot (bool): Log screenshot for debugging (default: None)
- _pause (bool): Apply global PAUSE after operation (default: True)
Returns:
Context manager
Example:
with pyautogui.hold('shift'):
pyautogui.press(['right', 'right', 'right']) # Select text
"""Utility functions to validate key names and check character requirements.
def isValidKey(key):
"""
Check if key name is valid for the current platform.
Parameters:
- key (str): Key name to validate
Returns:
bool: True if key is valid, False otherwise
"""
def isShiftCharacter(character):
"""
Check if character requires shift key to type.
Parameters:
- character (str): Single character to check
Returns:
bool: True if character requires shift, False otherwise
"""PyAutoGUI supports the following key names (case-insensitive):
# All letters a-z (case insensitive)
KEY_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']# Number keys 0-9
KEY_NUMBERS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']# Function keys F1-F24
FUNCTION_KEYS = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10',
'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19',
'f20', 'f21', 'f22', 'f23', 'f24']MODIFIER_KEYS = [
'shift', 'shiftleft', 'shiftright',
'ctrl', 'ctrlleft', 'ctrlright', 'control',
'alt', 'altleft', 'altright', 'option',
'cmd', 'command', 'win', 'winleft', 'winright'
]NAVIGATION_KEYS = [
'up', 'down', 'left', 'right', # Arrow keys
'home', 'end', 'pageup', 'pagedown', # Page navigation
'tab', 'enter', 'return', 'space', # Common navigation
'backspace', 'delete', 'insert' # Editing keys
]SPECIAL_KEYS = [
'escape', 'esc', # Escape
'capslock', 'numlock', 'scrolllock', # Lock keys
'printscreen', 'pause', 'break', # System keys
'menu', 'apps', # Context menu keys
'select', 'execute', 'sleep' # Additional special keys
]NUMPAD_KEYS = [
'num0', 'num1', 'num2', 'num3', 'num4', # Numpad numbers
'num5', 'num6', 'num7', 'num8', 'num9',
'multiply', 'add', 'separator', # Numpad operators
'subtract', 'decimal', 'divide'
]# All valid key names are available in:
KEY_NAMES: List[str] # Complete list of all valid key names
KEYBOARD_KEYS: List[str] # Alias for KEY_NAMES (backwards compatibility)# Keyboard layout mappings for international support
QWERTY: str # QWERTY keyboard layout character mapping
QWERTZ: str # QWERTZ keyboard layout character mappingimport pyautogui
# Basic key pressing
pyautogui.press('enter') # Press Enter key
pyautogui.press('f1') # Press F1 function key
pyautogui.press('space', presses=3) # Press space 3 times
# Text typing
pyautogui.typewrite('Hello, World!') # Type text
pyautogui.typewrite('Slow typing', interval=0.1) # Type with delay
# Keyboard shortcuts
pyautogui.hotkey('ctrl', 'c') # Copy
pyautogui.hotkey('ctrl', 'v') # Paste
pyautogui.hotkey('alt', 'f4') # Close window
pyautogui.hotkey('ctrl', 'shift', 's') # Save As
# Advanced key control
pyautogui.keyDown('shift') # Hold shift
pyautogui.press(['right', 'right']) # Select text while holding
pyautogui.keyUp('shift') # Release shift
# Using context manager
with pyautogui.hold('ctrl'):
pyautogui.press('a') # Select all (Ctrl+A)
# Key validation
if pyautogui.isValidKey('f15'):
pyautogui.press('f15')
# Check if character needs shift
if pyautogui.isShiftCharacter('A'):
print("Capital A requires shift key")
# Complex typing with special keys
pyautogui.typewrite('Username: ')
pyautogui.typewrite('john_doe')
pyautogui.press('tab') # Move to next field
pyautogui.typewrite('password123')
pyautogui.press('enter') # Submit formInstall with Tessl CLI
npx tessl i tessl/pypi-pyautogui