Pig LogoPig Docs
SDK Reference/Python

Connections

The API for sending events to a machine.

Connection APIs

Connections provide the interface for interacting with a machine. Always use the context manager pattern when working with connections:

with machine.connect() as conn:
    # Your automation code here

Keyboard Control

# Type text
conn.type("Hello World")
 
# Press special keys
conn.key("super")                     # Press Windows key
 
# Key combinations
conn.key("ctrl+c ctrl+v")             # Copy and paste

Mouse Control

# Move cursor
conn.mouse_move(x=100, y=100)
 
# Click operations
conn.left_click()                     # Click at current position
conn.left_click(x=100, y=100)         # Move and click
conn.right_click(x=100, y=100)        # Right click
conn.double_click(x=100, y=100)       # Double click
 
# Drag operations
conn.left_click_drag(x=200, y=200)    # Click and drag

Screen Operations

# Take screenshot
image = conn.screenshot()
 
# Get cursor position
x, y = conn.cursor_position()
 
# Get machine dimensions
w, h = conn.dimensions()

Control Flow

These tools only apply to Remote machines.

# Give control to human
conn.yield_control()
 
# Wait for control back. You must manually unblock the bot in the Pig UI.
conn.await_control()

Best Practices

  1. Always use the context manager pattern (with machine.connect() as conn:) to ensure proper connection handling.
  2. Group related operations within a single connection for better performance.
  3. Use yield_control() and await_control() when you need human intervention during an automation sequence.

On this page