Python UIAutomation for Windows - comprehensive library for automating Windows applications using Microsoft's UIAutomation framework
Mouse and keyboard input simulation for automating user interactions with Windows applications. These functions provide low-level input capabilities that work across all Windows applications, supporting clicking, typing, dragging, and special key combinations.
Global mouse functions that operate at the screen coordinate level.
def Click(x: int, y: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Click the left mouse button at the specified screen coordinates.
Args:
x: X coordinate in screen pixels
y: Y coordinate in screen pixels
waitTime: Wait time after operation in seconds
"""
def RightClick(x: int, y: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Click the right mouse button at the specified screen coordinates.
Args:
x: X coordinate in screen pixels
y: Y coordinate in screen pixels
waitTime: Wait time after operation in seconds
"""
def MiddleClick(x: int, y: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Click the middle mouse button at the specified screen coordinates.
Args:
x: X coordinate in screen pixels
y: Y coordinate in screen pixels
waitTime: Wait time after operation in seconds
"""
def MoveTo(x: int, y: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Move the mouse cursor to the specified screen coordinates.
Args:
x: X coordinate in screen pixels
y: Y coordinate in screen pixels
waitTime: Wait time after operation in seconds
"""
def DragDrop(x1: int, y1: int, x2: int, y2: int, moveSpeed: int = 1, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Perform a drag and drop operation from one point to another.
Args:
x1: Starting X coordinate in screen pixels
y1: Starting Y coordinate in screen pixels
x2: Ending X coordinate in screen pixels
y2: Ending Y coordinate in screen pixels
moveSpeed: Speed of drag movement (1=slow, higher=faster)
waitTime: Wait time after operation in seconds
"""Global keyboard functions for sending keys and key combinations.
def KeyDown(key: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Press a key down (without releasing).
Args:
key: Virtual key code (use Keys constants)
waitTime: Wait time after operation in seconds
"""
def KeyUp(key: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Release a previously pressed key.
Args:
key: Virtual key code (use Keys constants)
waitTime: Wait time after operation in seconds
"""
def SendKey(key: int, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Send a single key (press and release).
Args:
key: Virtual key code (use Keys constants)
waitTime: Wait time after operation in seconds
"""
def SendKeys(keys: str, interval: float = 0.01, waitTime: float = OPERATION_WAIT_TIME, debug: bool = False) -> None:
"""
Send a sequence of keys from a string.
Args:
keys: String representing keys to send (supports special key notation)
interval: Time between individual key presses in seconds
waitTime: Wait time after operation in seconds
debug: Enable debug output for key sending
"""Functions for simulating mouse wheel scrolling.
def WheelDown(wheelTimes: int = 1, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Scroll the mouse wheel down.
Args:
wheelTimes: Number of wheel notches to scroll
waitTime: Wait time after operation in seconds
"""
def WheelUp(wheelTimes: int = 1, waitTime: float = OPERATION_WAIT_TIME) -> None:
"""
Scroll the mouse wheel up.
Args:
wheelTimes: Number of wheel notches to scroll
waitTime: Wait time after operation in seconds
"""class Keys:
"""Virtual key code constants for keyboard input."""
# Letter keys
VK_A: int = 65
VK_B: int = 66
# ... (VK_C through VK_Z)
VK_Z: int = 90
# Number keys
VK_0: int = 48
VK_1: int = 49
# ... (VK_2 through VK_9)
VK_9: int = 57
# Function keys
VK_F1: int = 112
VK_F2: int = 113
# ... (VK_F3 through VK_F12)
VK_F12: int = 123
# Navigation keys
VK_LEFT: int = 37
VK_UP: int = 38
VK_RIGHT: int = 39
VK_DOWN: int = 40
VK_HOME: int = 36
VK_END: int = 35
VK_PAGEUP: int = 33
VK_PAGEDOWN: int = 34
# Editing keys
VK_INSERT: int = 45
VK_DELETE: int = 46
VK_BACKSPACE: int = 8
VK_RETURN: int = 13
VK_ENTER: int = 13
VK_TAB: int = 9
VK_SPACE: int = 32
VK_ESCAPE: int = 27
# Modifier keys
VK_SHIFT: int = 16
VK_CONTROL: int = 17
VK_ALT: int = 18
VK_LWIN: int = 91
VK_RWIN: int = 92
# Lock keys
VK_CAPITAL: int = 20 # Caps Lock
VK_NUMLOCK: int = 144
VK_SCROLL: int = 145
# Numpad keys
VK_NUMPAD0: int = 96
VK_NUMPAD1: int = 97
# ... (VK_NUMPAD2 through VK_NUMPAD9)
VK_NUMPAD9: int = 105
VK_MULTIPLY: int = 106 # *
VK_ADD: int = 107 # +
VK_SEPARATOR: int = 108
VK_SUBTRACT: int = 109 # -
VK_DECIMAL: int = 110 # .
VK_DIVIDE: int = 111 # /class ModifierKey:
"""Modifier key constants for use with SendKey function."""
ALT: int = 1 # Alt key modifier
CONTROL: int = 2 # Ctrl key modifier
SHIFT: int = 4 # Shift key modifier
WIN: int = 8 # Windows key modifierFunctions for interacting with the Windows clipboard.
def GetClipboardText() -> str:
"""
Get text content from the Windows clipboard.
Returns:
str: Text content from clipboard, or empty string if no text
"""
def SetClipboardText(text: str) -> None:
"""
Set text content to the Windows clipboard.
Args:
text: Text to place on clipboard
"""import uiautomation
# Click at specific screen coordinates
uiautomation.Click(100, 200)
# Right-click to open context menu
uiautomation.RightClick(300, 400)
# Drag and drop operation
uiautomation.DragDrop(100, 100, 200, 200)
# Move cursor without clicking
uiautomation.MoveTo(150, 250)# Send individual keys
uiautomation.SendKey(uiautomation.Keys.VK_RETURN)
uiautomation.SendKey(uiautomation.Keys.VK_TAB)
# Send key combinations
uiautomation.SendKey(
uiautomation.Keys.VK_S,
uiautomation.ModifierKey.CONTROL # Ctrl+S
)
uiautomation.SendKey(
uiautomation.Keys.VK_C,
uiautomation.ModifierKey.CONTROL | uiautomation.ModifierKey.SHIFT # Ctrl+Shift+C
)
# Send text sequences
uiautomation.SendKeys("Hello World!")
uiautomation.SendKeys("user@example.com{TAB}password123{ENTER}")# Navigation sequences
uiautomation.SendKeys("{HOME}") # Go to beginning of line
uiautomation.SendKeys("{END}") # Go to end of line
uiautomation.SendKeys("{CTRL+A}") # Select all
uiautomation.SendKeys("{CTRL+C}") # Copy
uiautomation.SendKeys("{CTRL+V}") # Paste
# Function keys
uiautomation.SendKeys("{F5}") # Refresh
uiautomation.SendKeys("{ALT+F4}") # Close window
# Arrow key navigation
uiautomation.SendKeys("{DOWN 3}") # Press down arrow 3 times
uiautomation.SendKeys("{RIGHT 5}") # Press right arrow 5 times# Scroll down in a document or list
uiautomation.WheelDown(3) # Scroll down 3 notches
# Scroll up
uiautomation.WheelUp(5) # Scroll up 5 notches
# Scroll at specific location (move cursor first)
uiautomation.MoveTo(400, 300)
uiautomation.WheelDown(2)# Copy text to clipboard
uiautomation.SetClipboardText("Text to copy")
# Get text from clipboard
clipboard_content = uiautomation.GetClipboardText()
print(f"Clipboard contains: {clipboard_content}")
# Copy-paste workflow
uiautomation.SendKeys("{CTRL+A}") # Select all
uiautomation.SendKeys("{CTRL+C}") # Copy
original_text = uiautomation.GetClipboardText()
# Modify and paste back
modified_text = original_text.upper()
uiautomation.SetClipboardText(modified_text)
uiautomation.SendKeys("{CTRL+V}") # Paste# Hold key down for multiple operations
uiautomation.KeyDown(uiautomation.Keys.VK_SHIFT)
uiautomation.SendKey(uiautomation.Keys.VK_RIGHT) # Shift+Right (select)
uiautomation.SendKey(uiautomation.Keys.VK_RIGHT) # Shift+Right (select more)
uiautomation.KeyUp(uiautomation.Keys.VK_SHIFT)
# Complex application automation
def automate_text_editor():
# Open file dialog
uiautomation.SendKey(uiautomation.Keys.VK_O, uiautomation.ModifierKey.CONTROL)
# Type filename and open
uiautomation.SendKeys("document.txt{ENTER}")
# Select all and replace content
uiautomation.SendKeys("{CTRL+A}")
uiautomation.SendKeys("New content for the document")
# Save file
uiautomation.SendKey(uiautomation.Keys.VK_S, uiautomation.ModifierKey.CONTROL)import time
def safe_click(x, y, retries=3):
"""Safely click with retries."""
for attempt in range(retries):
try:
uiautomation.Click(x, y)
return True
except Exception as e:
print(f"Click attempt {attempt + 1} failed: {e}")
if attempt < retries - 1:
time.sleep(0.5) # Brief pause before retry
return False
def safe_send_keys(keys, retries=3):
"""Safely send keys with retries."""
for attempt in range(retries):
try:
uiautomation.SendKeys(keys)
return True
except Exception as e:
print(f"SendKeys attempt {attempt + 1} failed: {e}")
if attempt < retries - 1:
time.sleep(0.5)
return FalseInstall with Tessl CLI
npx tessl i tessl/pypi-uiautomation