利用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)

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

相关推荐
源码方舟6 分钟前
【基于ALS模型的教育视频推荐系统(Java实现)】
java·python·算法·音视频
萑澈17 分钟前
2025深圳杯D题法医物证多人身份鉴定问题四万字思路
python·数学建模
Ronin-Lotus19 分钟前
图像处理篇---MJPEG视频流处理
图像处理·python·opencv
请你喝好果汁64138 分钟前
python_竞态条件
开发语言·python
正在走向自律40 分钟前
Python 数据分析与可视化:开启数据洞察之旅(5/10)
开发语言·人工智能·python·数据挖掘·数据分析
dudly1 小时前
Python 字典键 “三变一” 之谜
开发语言·python
小明.杨2 小时前
Django 中时区的理解
后端·python·django
陈奕昆2 小时前
五、【LLaMA-Factory实战】模型部署与监控:从实验室到生产的全链路实践
开发语言·人工智能·python·llama·大模型微调
程序猿小三2 小时前
python uv的了解与使用
开发语言·python·uv
T0uken2 小时前
【Python】UV:单脚本依赖管理
chrome·python·uv