Monday, January 20, 2014

Simple mouse event simulator with python

This is a simple python script that simulates mouse clicks. This script can do click,double click and multi-click operations of both right and left mouse buttons. I used python win32api (which can download from here) for develop this script.


import win32api, win32con, time 

def left_click(x,y):
 win32api.SetCursorPos((x,y))#set position to given 
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)#push the left mouse button
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)#relese the left mouse button


def right_click(x,y):
 win32api.SetCursorPos((x,y))#set position
 win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)#push the right mouse button
 win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)#relese the right mouse button

def multiple_click(x,y,times,left):#run the clicks
 if(left):#if left mouse button need to be clicked
  for _ in range(times):#clicke n timese where n =times
   left_click(x,y)#call function left_click
   time.sleep(0.1)#delay between each click
 elif(not left):#if right mouse button need to be clicked
  for _ in range(times):#click n times where n =times
   right_click(x,y)#call right_click event
   time.sleep(0.1)#delay between each click

#call functions for double click left mouse button where x= 10 and y = 10
multiple_click(10,10,2,True)
#call function for right click where x = 10 and y = 10
#multiple_click(10,10,1,False)

No comments:

Post a Comment