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的监视,笔者了解甚少,想看源代码,跳转后貌似看不出啥。

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

相关推荐
用户2519162427112 小时前
Python之语言特点
python
刘立军3 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机6 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机7 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i8 小时前
django中的FBV 和 CBV
python·django
c8i9 小时前
python中的闭包和装饰器
python
这里有鱼汤12 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩1 天前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python