How to detect mouse events (left click) with Python
You can use the win32api to check for any key presses, including mouse events.
- # Code to check if left or right mouse buttons were pressed
- import win32api
- import time
- state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128
- state_right = win32api.GetKeyState(0x02) # Right button down = 0 or 1. Button up = -127 or -128
- while True:
- a = win32api.GetKeyState(0x01)
- b = win32api.GetKeyState(0x02)
- if a != state_left: # Button state changed
- state_left = a
- print(a)
- if a < 0:
- print('Left Button Pressed')
- else:
- print('Left Button Released')
- if b != state_right: # Button state changed
- state_right = b
- print(b)
- if b < 0:
- print('Right Button Pressed')
- else:
- print('Right Button Released')
- time.sleep(0.001)
Articoli simili
- How to make the background image of a button disappear when we click it, and make it appear on another button when we click it in Visual Studio
- Come automatizzare il click su una pagina web usando Python
- How to detect an object from static image and crop it from the image using openCV
- Perché i mouse senza fili sono generalmente più piccoli di quelli cablati? Potete suggerirmi alcuni mouse wireless economici più grandi?