当前位置: 首页 > 工具软件 > python-mode > 使用案例 >

python-模拟键盘按键

姜松
2023-12-01

 

#! /usr/bin/env python
# coding:utf-8

import time
from pynput.keyboard import Key, Controller
from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(key.char))
    except AttributeError:
        print('special key {0} pressed'.format(key))


def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.esc:
        return False


def ListenerKey():
    while True:
        with keyboard.Listener( on_press=on_press,on_release=on_release) as listener:
            listener.join()


def sim_key_run():
    print(' 10s 后开始启动 ')
    keyboardCtl = Controller()

    for i in range(10):
        keyboardCtl.press(Key.right)
        print('i=',i)
        time.sleep(1)

    print('*'*30)


if __name__ == '__main__':
    sim_key_run()

 

 类似资料: