监听和模拟键盘和鼠标输入 https://github.com/moses-palmer/pynput

天问 f7c0232672 Update 'README.md' 1 week ago
README.md f7c0232672 Update 'README.md' 1 week ago

README.md

pynput

监听和模拟键盘和鼠标输入

Usage

from pynput import keyboard

controller = keyboard.Controller()

# 定义回调函数,处理按键事件
def on_press(key):
    try:
        print('按键按下: {0}'.format(key.char))
    except AttributeError:
        print('特殊按键按下: {0}'.format(key))

def on_release(key):
    print('按键释放: {0}'.format(key))
    if key == keyboard.Key.esc:
        # 返回 False 停止监听
        return False

# 监听键盘事件
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()


controller.press(keyboard.Key.media_volume_up)
controller.release(keyboard.Key.media_volume_up)