Advertisement
Guest User

Mouse Lib

a guest
May 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. import win32gui, win32api, win32con, ctypes
  2.  
  3. class Mouse:
  4.     """It simulates the mouse"""
  5.     MOUSEEVENTF_MOVE = 0x0001 # mouse move
  6.     MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
  7.     MOUSEEVENTF_LEFTUP = 0x0004 # left button up
  8.     MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
  9.     MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
  10.     MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
  11.     MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
  12.     MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
  13.     MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
  14.     SM_CXSCREEN = 0
  15.     SM_CYSCREEN = 1
  16.  
  17.     def _do_event(self, flags, x_pos, y_pos, data, extra_info):
  18.         """generate a mouse event"""
  19.         x_calc = 65536 * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
  20.         y_calc = 65536 * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
  21.         return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)
  22.  
  23.     def _get_button_value(self, button_name, button_up=False):
  24.         """convert the name of the button into the corresponding value"""
  25.         buttons = 0
  26.         if button_name.find("right") >= 0:
  27.             buttons = self.MOUSEEVENTF_RIGHTDOWN
  28.         if button_name.find("left") >= 0:
  29.             buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
  30.         if button_name.find("middle") >= 0:
  31.             buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
  32.         if button_up:
  33.             buttons = buttons << 1
  34.         return buttons
  35.  
  36.     def move_mouse(self, pos):
  37.         """move the mouse to the specified coordinates"""
  38.         (x, y) = pos
  39.         old_pos = self.get_position()
  40.         x =  x if (x != -1) else old_pos[0]
  41.         y =  y if (y != -1) else old_pos[1]    
  42.         self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)
  43.  
  44.     def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
  45.         """push a button of the mouse"""
  46.         self.move_mouse(pos)
  47.         self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)
  48.  
  49.     def click(self, pos=(-1, -1), button_name= "left"):
  50.         """Click at the specified placed"""
  51.         self.move_mouse(pos)
  52.         self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)
  53.  
  54.     def double_click (self, pos=(-1, -1), button_name="left"):
  55.         """Double click at the specifed placed"""
  56.         for i in range(2):
  57.             self.click(pos, button_name)
  58.  
  59.     def get_position(self):
  60.         """get mouse position"""
  61.         return win32api.GetCursorPos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement