pynput 同时监听键盘及鼠标

#方法5s执行不完,自动放弃执行
@func_set_timeout(5)
def on_key_q(x,y):
    #耗时操作完成后通知主线程更新ui
    wx.CallAfter(top.update_toast, result,toast_alpha)#子线程通知更新弹窗


def on_key_click(key):
    '''
    键盘事件
    :return:
    '''
    container = list(pyautogui.position())
    key=str(key)[1]
    if  key == "q":
        # print(key)
        try:
            # 线程池执行操作
            executor.submit(on_key_q,container[0],container[1])
        except func_timeout.exceptions.FunctionTimedOut as e:
            writeLog("方法超时,关闭键盘监听")#写入日志方法
            return False
        except Exception as e:
           writeLog(traceback.format_exc())#写入日志方法

#方法3s执行不完,自动放弃执行
@func_set_timeout(3)
def mouse_left_pressed():
    #耗时操作



mkdir(image_save_path)
def on_mouse_click(x, y, click, pressed):
    '''
   鼠标事件
   :return:
   '''
    if  click == mouse.Button.left and pressed:
        try:
            mouse_left_pressed()
        except func_timeout.exceptions.FunctionTimedOut as e:
            writeLog("方法超时,关闭鼠标监听")
            return False
        except Exception as e:
            writeLog(traceback.format_exc())

def func_key_click():
    '''
    监听键盘
    :return:
    '''
    with  keyboard.Listener(on_press=on_key_click) as keyboardListener:
        func_mouse_click()#鼠标事件
        app.MainLoop()#主线程创建UI
        keyboardListener.join()
def func_mouse_click():
    '''
    监听鼠标
    :return:1234
    '''
    mouse_listener = mouse.Listener(on_click=on_mouse_click,on_move=None,on_scroll=None)
    mouse_listener.start()

if __name__ == "__main__":
   func_key_click()#键盘监听阻塞式,鼠标监听非阻塞式,UI放在主线程
遇到的问题:

1.鼠标和键盘一起阻塞式监听,会导致界面卡顿

2.子线程创建更新UI会导致程序突然崩溃

3.鼠标键盘耗时操作会界面卡顿

你可能感兴趣的:(python)