python控制鼠标键盘+监听键盘

import pyautogui
from pynput.keyboard import Controller, Key, Listener


# 监听按压
def on_press(key):
    try:
        print("正在按压:", format(key.char))
    except AttributeError:
        print("正在按压:", format(key))


# 监听释放
def on_release(key):
    print("已经释放:", format(key))

    if key == Key.esc:
        # 停止监听
        quit()


# 开始监听
def start_listen():
    with Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()


if __name__ == '__main__':
    # 实例化键盘
    kb = Controller()
    print(pyautogui.size())
    print(pyautogui.position())
    pyautogui.hotkey('alt', 'tab')
    i = 0

    pyautogui.hotkey('ctrl', 'v')
    # while i < 20:
    #      pyautogui.hotkey('ctrl', 'v')
    #      if i>=10:
    #         pyautogui.press(str(int(i/10)))
    #         pyautogui.press(str(i%10))
    #      else:
    #         pyautogui.press(str(i))
    #      pyautogui.hotkey('alt', 's')
    #      i += 1
    #开始监听,按esc退出监听
    start_listen()

 

posted @ 2021-06-29 17:18  陈扬天  阅读(2015)  评论(0编辑  收藏  举报