Python realizes the monitoring of mouse dragging

Table of contents

module preparation

Specific steps 

Set watch function

call monitor

Notice

conclusion


module preparation

from pynput.mouse import Listener

This is used to monitor the mouse, and the pynput module also monitors the keyboard.


Specific steps 

        First, set a global variable that is used to record mouse presses and releases

drag_flag = True

Set watch function

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

        Note: There are four parameters in the on_click function here, you don’t have to use them, but you have to define them, otherwise the code will report an error later.

        ①First set the drag_flag , mouse_press , mouse_release parameters as global variables, so that the code can be used later. If the mouse is pressed, the monitor will monitor the position of the mouse pressed, and we can print the output. Here we use mouse_press to accept this coordinate and set drag_flag is set to False ; similarly, if the mouse is not pressed, we use mouse_release to receive its release position and set drag_flag to True ;

        ② Then judge whether drag_flag is True , if yes, execute the judgment statement, judge whether mouse_press is equal to mouse_release , that is, whether the coordinate positions of pressing and releasing are consistent, if they are consistent, it means mouse click, otherwise it is mouse dragging, here You can add the functions you want by yourself. If drag_flag is False , execute the else statement, pass directly skip.

call monitor

pynput comes with monitoring of the mouse and keyboard, we can use it directly as follows

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

Notice

The monitoring will always execute the on_click function, so if there is no drag_flag , it is like the following

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("这是鼠标点击")

        After running the first if, it will immediately enter the second if, that is, you just pressed the mouse, and after printing the pressed coordinate position, immediately enter to judge whether the coordinates of the mouse press and release are the same, although I did not give mouse_release here The parameter sets the initial value, but here the coordinates of pressing and releasing are generally different, so it will print "this is mouse dragging", but at this time we just press the mouse without dragging, so it is not us function to be implemented.

But adding drag_flag and setting it to False         after pressing , and setting to True after releasing can solve this problem very well, because after pressing, drag_flag=False , you can’t enter the if judgment that judges the comparison between the pressed coordinates and the released coordinates, only when you release After the mouse, drag_flag=True , and then enter this judgment, and finally realize the function we want.

The whole code is as follows:

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()

conclusion

        The author thought that he needed a text that could get the text selected by the mouse, but found that neither python nor win32 could implement such an interface, so he thought about how to implement it. The simpler way is to monitor the mouse press and release, but the Internet has no support for this. , There are few people who explain it. What I talk about here is only what I have practiced. I know very little about the monitoring of pynput . I want to see the source code, but I don’t seem to see anything after the jump.

        We will continue to update this content in the future! !

Guess you like

Origin blog.csdn.net/knighthood2001/article/details/132293026