利用python进行键盘模拟输入
记一次利用python模拟键盘输入,由于键盘中英文切换较为麻烦,所以写了两个小程序分别进行英文字符模拟或中文字符模拟。
#用于键盘英文字符输入模拟
import pyautogui
import timedef simulate_typing(text):# Give some time to switch to the desired applicationtime.sleep(5)# Simulate typing each characterfor char in text:pyautogui.typewrite(char)time.sleep(0.1) # Adjust the delay between keystrokes if needed# Press Enter key at the endpyautogui.press('enter')# 要输入的英文字符
input_text = input("Enter the text : ")
simulate_typing(input_text)
#用于键盘中文字符输入模拟
from pynput.keyboard import Controller
import timedef 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)
在第二个程序中,也可以输入英文字符,但是由于系统键盘中英文的切换问题,当进行英文和中文混合使用时,模拟会出现偏差。