利用python进行键盘模拟输入

记一次利用python模拟键盘输入,由于键盘中英文切换较为麻烦,所以写了两个小程序分别进行英文字符模拟或中文字符模拟。

python 复制代码
#用于键盘英文字符输入模拟
import pyautogui
import time


def simulate_typing(text):
    # Give some time to switch to the desired application
    time.sleep(5)

    # Simulate typing each character
    for char in text:
        pyautogui.typewrite(char)
        time.sleep(0.1)  # Adjust the delay between keystrokes if needed

    # Press Enter key at the end
    pyautogui.press('enter')


# 要输入的英文字符
input_text = input("Enter the text : ")
simulate_typing(input_text)
python 复制代码
#用于键盘中文字符输入模拟
from pynput.keyboard import Controller
import time


def type_chinese_text(text):
    keyboard = Controller()
    time.sleep(5)
    for char in text:
        keyboard.type(char)
        time.sleep(0.1)  # Adjust the delay between keystrokes if needed


# 要输入的中文字符
input_text = input("Enter the text : ")

type_chinese_text(input_text)

在第二个程序中,也可以输入英文字符,但是由于系统键盘中英文的切换问题,当进行英文和中文混合使用时,模拟会出现偏差。

相关推荐
花酒锄作田1 小时前
[MCP][01]简介与概念
python·llm·mcp
Python私教1 小时前
Django全栈班v1.04 Python基础语法 20250912 上午
后端·python·django
言之。1 小时前
Django REST框架:ModelViewSet全面解析
数据库·python·django
Pocker_Spades_A2 小时前
Python快速入门专业版(二十六):Python函数基础:定义、调用与返回值(Hello函数案例)
开发语言·python
周周记笔记2 小时前
学习笔记:Python的起源
开发语言·python
魂尾ac3 小时前
Django + Vue3 前后端分离技术实现自动化测试平台从零到有系列 <第一章> 之 注册登录实现
后端·python·django·vue
Source.Liu3 小时前
【Pywinauto库】10.7 pywinauto.controls.uia_controls控件
windows·python·自动化
人工干智能3 小时前
建自己的Python项目仓库,使用工具:GitHub(远程仓库)、GitHub Desktop(版本控制工具)、VSCode(代码编辑器)
python·编辑器·github
StarPrayers.3 小时前
PySpark基础知识(python)
python·数据分析·spark
潜龙95274 小时前
第6.2节 Android Agent开发<二>
android·python·覆盖率数据