Python 模拟按键

模块

定时循环按键

import win32api
import win32con 
import time
import datetime

print("start!")
time.sleep(10)
n = 1
while True:
    win32api.keybd_event(49,0,0,0) 
    time.sleep(0.2)
    win32api.keybd_event(49,0,win32con.KEYEVENTF_KEYUP,0) 
    if n%10 == 0:
        win32api.keybd_event(50,0,0,0) 
        time.sleep(0.2)
        win32api.keybd_event(50,0,win32con.KEYEVENTF_KEYUP,0) 
    print(str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+" loop: " + str(n))
    n = n + 1
    time.sleep(1)

虚拟按键码