# Typing Speed Test Bot

This utility automates keyboard typing to simulate high-speed data entry. It is designed to type text from a string or file into any active text field (like a browser-based typing test) at a specified Words Per Minute (WPM) rate.

**Modules Used:**
*   [[programming/python/modules/pyautogui-module|pyautogui]]: To simulate keyboard presses.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.
*   `time`: To handle delays and countdowns.

## Installation

```bash
pip install pyautogui
```

## The Code

Save this as `type_bot.py`.

```python
import pyautogui
import time
import argparse
import os
import sys

def type_text(text, wpm, start_delay):
    # Safety feature: Drag mouse to corner to stop
    pyautogui.FAILSAFE = True
    
    # Calculate interval based on WPM
    # Standard word = 5 characters
    # Interval = 60 seconds / (WPM * 5 characters) = 12 / WPM
    if wpm > 0:
        interval = 12 / wpm
    else:
        interval = 0 # As fast as possible

    print(f"Target Speed: {wpm} WPM (Interval: {interval:.4f}s)")
    print(f"Starting in {start_delay} seconds...")
    print(">> SWITCH TO YOUR TARGET WINDOW NOW <<")
    
    try:
        for i in range(start_delay, 0, -1):
            print(f"{i}...", end=" ", flush=True)
            time.sleep(1)
        print("\nGO!")

        # Type the text
        pyautogui.write(text, interval=interval)
        
        print("\nFinished typing.")

    except pyautogui.FailSafeException:
        print("\nFail-safe triggered from mouse corner. Stopped.")
    except KeyboardInterrupt:
        print("\nStopped by user.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Typing Speed Test Bot")
    parser.add_argument("input", help="Text string OR path to a text file")
    parser.add_argument("--wpm", type=int, default=100, help="Target Words Per Minute (default: 100)")
    parser.add_argument("--delay", type=int, default=5, help="Countdown delay in seconds (default: 5)")
    
    args = parser.parse_args()
    
    # Determine if input is a file or raw text
    content = args.input
    if os.path.exists(args.input) and os.path.isfile(args.input):
        try:
            with open(args.input, 'r', encoding='utf-8') as f:
                content = f.read()
            print(f"Loaded {len(content)} characters from '{args.input}'")
        except Exception as e:
            print(f"Error reading file: {e}")
            sys.exit(1)
            
    type_text(content, args.wpm, args.delay)
```

## Usage

1.  **Prepare your text**: Copy the text you need to type into a file (e.g., `text.txt`) or have it ready as a string.
2.  **Run the script**:
    ```bash
    # Type from a file at 120 WPM
    python type_bot.py text.txt --wpm 120
    
    # Type a short phrase instantly (0 delay between keys)
    python type_bot.py "The quick brown fox" --wpm 0
    ```
3.  **Focus**: Immediately click on the text box in the target application (e.g., browser) before the countdown ends.

[[programming/python/python]]