# Python PyAutoGUI Module

PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. It works on Windows, macOS, and Linux.

## Installation

```bash
pip install pyautogui
```

## Importing the Module

```python
import pyautogui
```

## Safety Features

### Fail-Safe

If your script goes out of control, you can move the mouse cursor to any of the four corners of the primary screen to trigger a `pyautogui.FailSafeException`, stopping the script.

```python
pyautogui.FAILSAFE = True # Enabled by default
```

### Pauses

You can add a delay after each PyAutoGUI call to give the application time to process the input.

```python
pyautogui.PAUSE = 2.5 # 2.5 seconds pause
```

## Mouse Control

### Getting Screen Size and Position

```python
width, height = pyautogui.size()
x, y = pyautogui.position()
print(f"Screen: {width}x{height}, Mouse: ({x}, {y})")
```

### Moving the Mouse

```python
# Move to absolute coordinates (x, y) over 'duration' seconds
pyautogui.moveTo(100, 100, duration=1)

# Move relative to current position
pyautogui.moveRel(0, 50, duration=1) # Move down 50 pixels
```

### Clicking and Dragging

```python
pyautogui.click()          # Click at current position
pyautogui.click(200, 200)  # Click at specific coordinates
pyautogui.doubleClick()
pyautogui.rightClick()

# Drag mouse to coordinates
pyautogui.dragTo(300, 300, duration=1)
```

## Keyboard Control

### Typing Strings

```python
# Type with a delay between key presses
pyautogui.write('Hello world!', interval=0.25)
```

### Pressing Keys

```python
pyautogui.press('enter')
pyautogui.press(['left', 'left', 'left']) # Press left arrow 3 times
```

### Hotkeys

```python
# Simulates holding Ctrl, pressing c, then releasing both
pyautogui.hotkey('ctrl', 'c') 
```

## Message Boxes

PyAutoGUI provides simple message box functions.

```python
pyautogui.alert('This is an alert box.')
response = pyautogui.confirm('Shall I proceed?')
print(response) # 'OK' or 'Cancel'
```

## Screenshot Functions

PyAutoGUI can take screenshots and locate images on the screen.

### Taking a Screenshot

```python
im = pyautogui.screenshot()
im.save('screenshot.png')
```

### Locating Images

You can find coordinates of an image on the screen. This is useful for finding buttons.

```python
# Returns (left, top, width, height) of the first match
button_location = pyautogui.locateOnScreen('submit_button.png')

if button_location:
    print(f"Button found at: {button_location}")
    pyautogui.click(pyautogui.center(button_location))
```

[[programming/python/python]]