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

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

相关推荐
0思必得08 分钟前
[Web自动化] Selenium执行JavaScript语句
前端·javascript·爬虫·python·selenium·自动化
焱童鞋11 分钟前
解决 MeteoInfoLab 3.9.11 中 contourfm 导致的 ArrayIndexOutOfBoundsException
开发语言·python
封奚泽优17 分钟前
化学配对记忆游戏:用Python和Pygame打造趣味化学学习工具
python·pygame
梦幻精灵_cq23 分钟前
问题切入『视角很重要』——ansi-color有效编码序列“单背景判定”小部件的“简洁精妙”
python
有代理ip23 分钟前
成功请求的密码:HTTP 2 开头响应码深度解析
java·大数据·python·算法·php
0思必得024 分钟前
[Web自动化] Selenium截图
前端·爬虫·python·selenium·自动化
放飞自我的Coder1 小时前
【PDF拆分 Python拆分左右并排PDF】
python·pdf
nimadan121 小时前
**AI漫剧爆款生成器2025推荐,解锁高互动率与平台适配的
人工智能·python