模拟键盘输入(支持中文)(支持不让粘贴的输入)
程序启动后 按快捷键,ctrl + alt + 9 可模拟键盘操作,快速输入剪切板中的内容。
import pyperclip
import time
import keyboard
import datetime
class KeyboardController:
def __init__(self):
self.ctrl_pressed = False
self.alt_pressed = False
def set_ctrl_pressed(self, value):
self.ctrl_pressed = value
def set_alt_pressed(self, value):
self.alt_pressed = value
def type_chinese_text():
time.sleep(0.5) # 等待输入法切换生效
clipboard_content = pyperclip.paste()
keyboard.write(clipboard_content)
# for key in clipboard_content:
# time.sleep(0.02) # 设置输入间隔为0.1秒
# keyboard.write(key)
current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("输入完成", "当前时间:", formatted_time)
def start_execution():
keyboard.add_hotkey('ctrl+alt+9', type_chinese_text)
keyboard.wait()
start_execution()