如何知道是否按下了鼠标左键

Cod*_*ife 3 python pyautogui

我正在使用PyAutoGUI库。我怎么知道是否按下了鼠标左键?

这就是我想做的:

if(leftmousebuttonpressed):
   print("left")
else:
   print("nothing")
Run Code Online (Sandbox Code Playgroud)

Al *_*art 7

(我是PyAutoGUI的作者。)我可以确认当前PyAutoGUI无法读取/记录单击或击键。这些功能都在路线图上,但目前没有任何资源或时间表专门用于这些功能。

  • 很不幸的是,不行。我建议在https://pypi.org/project/pynput/上使用pynput模块,以获得可以记录鼠标输入的模块。 (2认同)

Ali*_*jad 7

如何知道是否按下了鼠标左键?

受原始文档启发的简单版本代码:

from pynput import mouse

def on_click(x, y, button, pressed):
    if button == mouse.Button.left:
        print('{} at {}'.format('Pressed Left Click' if pressed else 'Released Left Click', (x, y)))
        return False # Returning False if you need to stop the program when Left clicked.
    else:
        print('{} at {}'.format('Pressed Right Click' if pressed else 'Released Right Click', (x, y)))

listener = mouse.Listener(on_click=on_click)
listener.start()
listener.join()
Run Code Online (Sandbox Code Playgroud)

就像Al Sweigart 爵士在上面的评论中提到的那样,我寻找了运行完美的pynput模块。请参阅文档PyPi说明:

使用 pip 安装库:pip install pynput

监控其他事件(例如鼠标移动、单击、滚动)

请参阅原始文档中 “监视鼠标标题”下的代码。https://pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse