我正在使用PyAutoGUI库。我怎么知道是否按下了鼠标左键?
这就是我想做的:
if(leftmousebuttonpressed):
print("left")
else:
print("nothing")
Run Code Online (Sandbox Code Playgroud)
(我是PyAutoGUI的作者。)我可以确认当前PyAutoGUI无法读取/记录单击或击键。这些功能都在路线图上,但目前没有任何资源或时间表专门用于这些功能。
受原始文档启发的简单版本代码:
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
| 归档时间: |
|
| 查看次数: |
4479 次 |
| 最近记录: |