Casa > H > How To Detect Mouse Events (Left Click) With Python

How to detect mouse events (left click) with Python

You can use the win32api to check for any key presses, including mouse events.

  1. # Code to check if left or right mouse buttons were pressed 
  2. import win32api 
  3. import time 
  4.  
  5. state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128 
  6. state_right = win32api.GetKeyState(0x02) # Right button down = 0 or 1. Button up = -127 or -128 
  7.  
  8. while True: 
  9. a = win32api.GetKeyState(0x01) 
  10. b = win32api.GetKeyState(0x02) 
  11.  
  12. if a != state_left: # Button state changed 
  13. state_left = a 
  14. print(a) 
  15. if a < 0: 
  16. print('Left Button Pressed') 
  17. else: 
  18. print('Left Button Released') 
  19.  
  20. if b != state_right: # Button state changed 
  21. state_right = b 
  22. print(b) 
  23. if b < 0: 
  24. print('Right Button Pressed') 
  25. else: 
  26. print('Right Button Released') 
  27. time.sleep(0.001) 

De Howland Merendino

Jogos como Clash Royale e Clash of Clans ficam aborrecidos depois de jogá-los por algum tempo? :: Por que muitos russos preferem o VK ao Facebook?