Python实现鼠标拖动的监视

目录

模块准备

具体步骤

设置监视函数

调用监视器

注意

结束语


模块准备

python 复制代码
from pynput.mouse import Listener

这是用来监视鼠标的,pynput模块中还有监视键盘的。


具体步骤

首先,设置一个全局变量,这个全局变量是用来记录鼠标按下和释放的

python 复制代码
drag_flag = True

设置监视函数

python 复制代码
def on_click(x, y, button, pressed):
    global drag_flag, mouse_press, mouse_release
    if pressed:
        print("按下", x, y)
        mouse_press = (x, y)
        drag_flag = False
    else:
        mouse_release = (x, y)
        print("释放", mouse_release)
        drag_flag = True
    if drag_flag:
        # 按下和释放的坐标不相等
        if mouse_press != mouse_release:
            print("这是鼠标拖动")
        else:
            print("这是鼠标点击")
    else:
        pass

注:这里on_click 函数里面有四个参数,你可以不去使用,但是得定义出来,否则后来代码会报错。

①首先将drag_flag , mouse_press , mouse_release 参数设置为全局变量,使得代码后面能使用,如果鼠标按下,监视器会监视鼠标按下的位置,我们可以打印输出,这里我们使用mouse_press 接受这个坐标,并将drag_flag 设置为False ;同样的,如果鼠标没有按下,我们用mouse_release 接收其释放位置,并将drag_flag 设置为True

②接着判断drag_flag 是否为True ,如果为是,则执行判断语句,判断mouse_press 是否等于mouse_release ,也就是按下和释放的坐标位置是否一致,如果一致,说明是鼠标点击,否则就是鼠标拖动,这里你可以自己加你想要的功能。如果drag_flag 是否为False ,执行else 语句,pass直接跳过。

调用监视器

pynput自带对鼠标和键盘的监视,我们直接如下使用就可以

python 复制代码
def main():
    with Listener(on_click=on_click) as listener:
        listener.join()

注意

监视会一直执行on_click 函数,因此如果没有drag_flag,也就是像下面这样

python 复制代码
def on_click(x, y, button, pressed):
    global mouse_press, mouse_release
    if pressed:
        print("按下", x, y)
        mouse_press = (x, y)
    else:
        mouse_release = (x, y)
        print("释放", mouse_release)

    if mouse_press != mouse_release:
        print("这是鼠标拖动")
    else:
        print("这是鼠标点击")

运行第一个if后会立刻进入第二个if,也就是你鼠标刚按下,打印了按下的坐标位置后,立刻进入判断鼠标按下和释放的坐标是否相同,虽然我这里没有给mouse_release参数设置初始值,但是这里按下坐标和释放坐标一般来说是不相同的,所以会打印"这是鼠标拖动",然而这时候我们仅仅是按下鼠标,没有进行拖动,所以不是我们要实现的功能。

但是加入drag_flag且在按下后置为False ,释放后置为True ,可以很好解决这个,因为按下后,drag_flag=False ,进入不了判断按下坐标和释放坐标比较的if判断,只有当释放鼠标后,drag_flag=True,然后才会进入这个判断,最终实现我们想要的功能。

全部代码如下:

python 复制代码
from pynput.mouse import Listener
# 全局变量
drag_flag = True
def on_click(x, y, button, pressed):
    global drag_flag, mouse_press, mouse_release
    if pressed:
        print("按下", x, y)
        mouse_press = (x, y)
        drag_flag = False
    else:
        mouse_release = (x, y)
        print("释放", mouse_release)
        drag_flag = True
    if drag_flag:
        # 按下和释放的坐标不相等
        if mouse_press != mouse_release:
            print("这是鼠标拖动")
        else:
            print("这是鼠标点击")
    else:
        pass

def main():
    with Listener(on_click=on_click) as listener:
        listener.join()

if __name__ == '__main__':
    main()

结束语

笔者想着是需要一个能获取鼠标选中的文字,但是发现pythonwin32 都没有能实现这样的接口,因此想着用什么方式实现呢,比较简单的就是监视鼠标按下和释放,但是网上对于这块,讲解的人较少,这里所讲的,也只是我实践出来的,对于pynput的监视,笔者了解甚少,想看源代码,跳转后貌似看不出啥。

后续会继续更新这方面的内容!!

相关推荐
PythonFun4 小时前
Python批量下载PPT模块并实现自动解压
开发语言·python·powerpoint
炼丹师小米4 小时前
Ubuntu24.04.1系统下VideoMamba环境配置
python·环境配置·videomamba
GFCGUO5 小时前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
985小水博一枚呀6 小时前
【深度学习基础模型】神经图灵机(Neural Turing Machines, NTM)详细理解并附实现代码。
人工智能·python·rnn·深度学习·lstm·ntm
萧鼎7 小时前
Python调试技巧:高效定位与修复问题
服务器·开发语言·python
IFTICing8 小时前
【文献阅读】Attention Bottlenecks for Multimodal Fusion
人工智能·pytorch·python·神经网络·学习·模态融合
大神薯条老师8 小时前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
程序员爱德华8 小时前
Python环境安装教程
python
huanxiangcoco8 小时前
152. 乘积最大子数组
python·leetcode
萧鼎8 小时前
Python常见问题解答:从基础到进阶
开发语言·python·ajax