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

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

相关推荐
大鹏说大话8 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
Niuguangshuo8 小时前
silero-vad:超轻量级开源 VAD 实践指南
开发语言·python
2501_944676169 小时前
揭秘!WORDTIP公司靠不靠谱?小白必看
大数据·人工智能·python
Minner-Scrapy9 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
CODER03049 小时前
Anaconda和Mamba创建环境管理包常用命令合集
python·深度学习·conda·虚拟环境·mamba·常用命令大全
闲猫10 小时前
LangGraph / Capabilities / Fault tolerance
python·agent·langgraph
IvanCodes10 小时前
Python 基础语法(一):变量、数据类型与运算符
python
程序员三藏11 小时前
浅谈性能测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
l12586511 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
meilindehuzi_a12 小时前
Python 基础语法(1):常量、变量、类型、输入输出与运算符
开发语言·python