famansour

WEBCODES # Python Check if mouse clicked

Aug 25th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. # Python Check if mouse clicked
  2. # https://stackoverflow.com/questions/41688871/python-check-if-mouse-clicked
  3.  
  4.  
  5. import win32api
  6. import time
  7.  
  8. width = win32api.GetSystemMetrics(0)
  9. height = win32api.GetSystemMetrics(1)
  10. midWidth = int((width + 1) / 2)
  11. midHeight = int((height + 1) / 2)
  12.  
  13. state_left = win32api.GetKeyState(0x01)  # Left button down = 0 or 1. Button up = -127 or -128
  14. while True:
  15.     a = win32api.GetKeyState(0x01)
  16.     if a != state_left:  # Button state changed
  17.         state_left = a
  18.         print(a)
  19.         if a < 0:
  20.             print('Left Button Pressed')
  21.         else:
  22.             print('Left Button Released')
  23.             win32api.SetCursorPos((midWidth, midHeight))
  24.     time.sleep(0.001)
Add Comment
Please, Sign In to add comment