這里我設定了一個變數 time_k 和 1 秒的多執行緒延遲變數來控制,
# -*- coding: UTF8 -*-
import PyHook3
import pythoncom
import threading
time_k = 0;
def execute_script(time_k_old, action):
'''
作用:執行腳本
'''
try:
global time_k
if(time_k ==1):
print(action + "單擊動作")
elif(time_k == 2):
print(action + "雙擊動作")
except Exception as e:
print(e)
time_k = 0;
# 監聽到滑鼠事件呼叫
def onMouseEvent(event):
global m
global time_k;
try:
if(event.MessageName != "mouse move" and (event.MessageName == "mouse left up" or event.MessageName == "mouse right up")): # 因為滑鼠一動就會有很多mouse move,所以把這個過濾下,滑鼠按下和抬起都會有記錄,這里我們把抬起down操作過濾掉
action = "" # 記錄左鍵還是右鍵點擊
if("right" in event.MessageName):
action = "右鍵"
elif("left" in event.MessageName):
action = "左鍵"
if(time_k == 0):
time_k = 1;
# 設定1秒后延遲執行
threading.Timer(1, execute_script, (time_k, action)).start()
elif(time_k == 1):
time_k = 2;
elif(time_k == 2):
return False
return True # 為True才會正常呼叫,如果為False的話,此次事件被攔截
except Exception as e:
print(e)
# 監聽到鍵盤事件呼叫
def onKeyboardEvent(event):
# print(event.Key) # 回傳按下的鍵
return True
def main():
# 創建管理器
hm = PyHook3.HookManager()
# 監聽鍵盤
hm.KeyDown = onKeyboardEvent
hm.HookKeyboard()
# 監聽滑鼠
hm.MouseAll = onm ouseEvent
hm.HookMouse()
# 回圈監聽
pythoncom.PumpMessages()
if __name__ == "__main__":
main()
效果圖如下:

喜歡的點個贊?吧!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/265391.html
標籤:python
上一篇:C/C++編程筆記:C++中strcat函式 VS strncat函式
下一篇:Pycharm初步上手
