pynput鼠标事件

import pynput

start = None
end = None

def on_click(x, y, button, pressed):
    global start, end
    print('{0} {1} at {2}'.format(button,'Pressed' if pressed else 'Released',(x, y)))
    if str(button) == "Button.left" and pressed:  # if mouse left button down
        start = (x,y)
    if not pressed and str(button) == "Button.left":  # if mouse left button up
        end = (x,y)
    if not pressed:
        # Stop listener
        return False

# Collect events until released
with pynput.mouse.Listener(on_click=on_click) as listener:
    listener.join()
print(f"Start coordinate:{start}, end coordinate:{end}")
posted @ 2021-07-22 13:25  wztshine  阅读(196)  评论(0编辑  收藏  举报