SoFunction
Updated on 2025-04-13

Detailed explanation of Python pyautogui's example of simulated keyboard input operations

1. Introduction

Today, when automating office and improving work efficiency, Python's pyautogui library has become our right-hand assistant for simulating keyboard and mouse operations. With pyautogui, we can easily implement automated GUI tasks such as automatically filling forms, automatically clicking buttons, etc., thus freeing our hands. This article will explain in detail how to use pyautogui to simulate keyboard input and provide actual code examples.

2. Detailed introduction to pyautogui library

1. Installation and configuration

First, we need to install the pyautogui library. It can be easily installed through the pip command:

pip install pyautogui

After the installation is complete, we can import the pyautogui module and configure some basic parameters as needed.

import pyautogui

# Turn off the automatic anti-malfunction function to prevent the program from being unable to stop = False

# Set a pause between all commands for one second = 1

2. Keyboard input function

pyautogui provides a variety of keyboard input functions to simulate keyboard operations:

Basic keyboard input:

('Hello world!', interval=0.25)

The above code will automatically enter "Hello world!" on the screen, with 0.25 seconds apart between each character.

Key operation:

('ctrl')
('c')
('ctrl')

This code simulates pressing the Ctrl key, then pressing the C key (copy operation), and finally releasing the Ctrl key.

Shortcut key operation:

('ctrl', 'c')

This line of code simulates the copy operation of Ctrl + C, making it more concise.

3. Special keys and combination keys

pyautogui supports a variety of special keys and key combinations. The following are some commonly used key representations:

# Special buttons('enter')  #Enter key('esc')   # Exit key('delete')  # Delete key
# Key combination('ctrl', 'v')  # Paste operation('ctrl', 'z')  # Undo operation('ctrl', 's')  # Save operation

3. Practical Examples

1. Automatically fill in the form

Suppose we need to automatically fill in a login form with the username and password, you can use the following code:

import pyautogui
import time

​​​​​​​# Wait for the page to load(2)

# Move to the username input box(100, 200)
# Enter username('myusername', interval=0.1)

# Move to the password input box(100, 250)
# Enter your password('mypassword', interval=0.1)

# Submit the form('enter')

2. Automatically send emails

Using pyautogui to simulate keyboard input, we can automate the process of sending emails:

import pyautogui
import time

​​​​​​​# Open the email client (assuming it is already open)(2)

# Fill in the recipient('recipient@', interval=0.1)
('tab')

# Fill in the email subject('Subject: Automated Email', interval=0.1)
('tab')

# Fill in the email content('This is an automated email sent using pyautogui.', interval=0.1)
('enter')

3. Automatic data entry

When processing large amounts of data entry, pyautogui can help us automate this process:

import pyautogui
import time

data = [
    {'name': 'John Doe', 'email': 'john@'},
    {'name': 'Jane Doe', 'email': 'jane@'}
]

​​​​​​​for item in data:
    # Click the Add New Contact button    (100, 300)
    (0.5)
    
    # Fill in your name    (item['name'], interval=0.1)
    ('tab')
    
    # Fill in your email address    (item['email'], interval=0.1)
    ('enter')

4. Summary

By using the pyautogui library, we can easily automate keyboard input, saving time and improving efficiency. Whether it is automatically filling out forms, sending emails or other repetitive tasks, pyautogui can be our right-hand assistant to free our hands. Hope this article helps you master the basic usage of pyautogui and apply it to actual automation tasks.

The above is a detailed explanation of the examples of Python pyautogui simulated keyboard input operations. For more information about Python pyautogui simulated keyboard, please pay attention to my other related articles!