前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 技术篇-基于PyHook3+threading多线程实现鼠标单击事件和双击事件的识别实例演示

Python 技术篇-基于PyHook3+threading多线程实现鼠标单击事件和双击事件的识别实例演示

作者头像
小蓝枣
发布2021-12-01 10:42:58
1.6K0
发布2021-12-01 10:42:58
举报
文章被收录于专栏:CSDN博客专家-小蓝枣的博客

这里我设定了一个变量 time_k1 秒的多线程延迟变量来控制。

代码语言:javascript
复制
# -*- 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 = onMouseEvent   
    hm.HookMouse()
    # 循环监听
    pythoncom.PumpMessages() 
 
if __name__ == "__main__":
    main()

效果图如下:

在这里插入图片描述
在这里插入图片描述

喜欢的点个赞❤吧!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档