English translation
Wait 2 seconds to switch to the target window
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous tutorial, we covered how to install and configure PyAutoGUI. Now, we’ll dive deeper into using PyAutoGUI for fundamental desktop application automation tasks. With these basic operations, you can automate simple, repetitive tasks—boosting your productivity.
Overview of PyAutoGUI Basic Operations
PyAutoGUI is a powerful Python library for simulating mouse and keyboard actions. It enables automation of desktop applications, making repetitive tasks significantly easier. Below, we demonstrate common operations such as moving the mouse, clicking, typing text, and triggering keyboard shortcuts.
1. Importing the Library
Before using PyAutoGUI, import it into your script. Ensure you’ve completed the installation steps from the previous lesson.
import pyautogui
import time
2. Moving the Mouse
You can move the mouse using the moveTo() and move() methods:
moveTo(x, y, duration): Moves the mouse to screen coordinates(x, y)overdurationseconds (optional).move(dx, dy, duration): Moves the mouse relative to its current position bydxpixels horizontally anddypixels vertically.
Example Code:
# Wait 2 seconds to switch to the target window
time.sleep(2)
# Move mouse to screen coordinates (100, 100)
pyautogui.moveTo(100, 100, duration=1)
3. Clicking the Mouse
The click() method simulates mouse clicks. You can specify left-click (default) or right-click.
Example Code:
# Left-click (no arguments needed)
pyautogui.click()
# Right-click
pyautogui.click(button='right')
4. Typing Text
Use typewrite() to input text rapidly. It supports an interval parameter to control the delay between keystrokes.
Example Code:
# Type text with a 0.25-second delay between each character
# (Wait 1 second first to ensure the target text field is focused)
pyautogui.typewrite('Hello, World!', interval=0.25)
5. Keyboard Shortcuts
The hotkey() method simulates simultaneous key presses—for example, copy (Ctrl+C) or paste (Ctrl+V).
Example Code:
# Simulate Ctrl+C (copy)
pyautogui.hotkey('ctrl', 'c')
# Simulate Ctrl+V (paste)
pyautogui.hotkey('ctrl', 'v')
6. Taking Screenshots
Although our focus is automation, PyAutoGUI also supports screenshot capture—a valuable tool for debugging and verifying automation results.
# Capture and save a screenshot
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')
7. Complete Example
Below is a full working example that opens Notepad (on Windows), types text, and saves the file.
import pyautogui
import time
import subprocess
# Launch Notepad (Windows only)
subprocess.Popen(['notepad.exe'])
time.sleep(2)
# Type text with 0.1-second intervals between keystrokes
pyautogui.typewrite('This is an automated text input example!', interval=0.1)
# Trigger Ctrl+S to open the Save dialog
pyautogui.hotkey('ctrl', 's')
time.sleep(1)
# Type filename and confirm with Enter
pyautogui.typewrite('example.txt', interval=0.1)
pyautogui.press('enter')
Summary
In this lesson, we explored core PyAutoGUI operations—including mouse movement, clicking, text input, and keyboard shortcut simulation. These fundamentals lay the groundwork for more advanced capabilities like image recognition and region-based interaction.
In the next tutorial, we’ll delve into PyAutoGUI’s image recognition features to enable more sophisticated, context-aware automation.
We hope this introduction helps you get started with PyAutoGUI and simplifies your desktop automation workflows! Feel free to ask questions if anything is unclear.
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Wait 2 seconds to switch to the target window?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue