2025-03-23 00:06:07 +08:00

28 lines
739 B
Python

import time
from pynput.keyboard import Controller
import threading
# 创建键盘控制器
keyboard = Controller()
def press_keys():
while True: # 持续循环
keyboard.press('1') # 按下 'w' 键
time.sleep(0.5) # 等待0.5秒
keyboard.release('w') # 释放 'w' 键
keyboard.press('d') # 按下 'd' 键
time.sleep(0.5) # 等待0.5秒
keyboard.release('d') # 释放 'd' 键
# 创建并启动线程
thread = threading.Thread(target=press_keys)
thread.daemon = True # 设置为守护线程
thread.start()
# 主线程可以在这里执行其他操作
try:
while True:
time.sleep(1) # 主线程保持运行
except KeyboardInterrupt:
print("程序已停止。")