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)
Artigos semelhantes
- 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
- How to detect an object from static image and crop it from the image using openCV
- É verdade que o verdadeiro nome do Mickey Mouse é Michael Theodore Mouse?
- How to add an HTML code to my website so that when people click it, it opens a WhatsApp conversation with me