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

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

相关推荐
集成显卡5 小时前
使用 Google 开源 AI 工具 LangExtract 进行结构化信息抽取
python·google·openai
久笙&5 小时前
对象存储解决方案:MinIO 的架构与代码实战
数据库·python·架构
不甘懦弱5 小时前
阿里云搭建flask服务器
服务器·python·flask
赵英英俊5 小时前
Python day51
人工智能·pytorch·python
律品5 小时前
pytest的前置与后置
开发语言·python·pytest
飞翔的佩奇5 小时前
【完整源码+数据集+部署教程】遥感森林砍伐检测系统源码和数据集:改进yolo11-SWC
python·yolo·计算机视觉·数据集·yolo11·遥感森林砍伐检测
阿汤哥的程序之路6 小时前
Python如何将两个列表转化为一个字典
python
RabbitYao6 小时前
Android 项目 通过 AndroidStringsTool 更新多语言词条
android·python
RabbitYao6 小时前
使用 Gemini 及 Python 更新 Android 多语言 Excel 文件
android·python
天天进步20156 小时前
Python机器学习入门:用scikit-learn构建你的第一个预测模型
python·机器学习·scikit-learn