我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用win32api.mouse_event()。
def Menu(): try: #?? pdhk=win32gui.GetWindowRect(win32gui.FindWindowEx(win32gui.FindWindow('TdxW_MainFrame_Class','???????V7.35 - [???-?????]'),None,'#32770',None)) win32api.SetCursorPos([pdhk[0]+330,pdhk[1]+10]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) #????? win32api.SetCursorPos([pdhk[0]+380,pdhk[1]+20+480]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #???????->?????
def Free_quotation(): time.sleep(3) try: Tab_handle = win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????V7.35'),None,'SysTabControl32','Tab1') #print(hex(Tab_handle)) #time.sleep(1) p=win32gui.GetWindowRect(Tab_handle) #print(p) #print(p[2]) #print(p[3]) win32api.SetCursorPos([p[0]+170,p[1]+7]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????V7.35'),None,'Button','??'),win32con.BM_CLICK,0,0) time.sleep(3) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #?????
def click_CurrentPlace(self,lb_dx,lb_dy): tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) win32api.SetCursorPos(tmp) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) #----------------------------------------------------------------------------- # Name: select_ssid # purpose: select ssid. # explain: # Author: yuanwen # # Created: 2013/07/5 #-----------------------------------------------------------------------------
def selectComboboxItemThird(hwnd, item): try: # item is an index Use this to select 0 + item win32gui.SendMessage(hwnd, win32con.CB_SHOWDROPDOWN, 1, 0) win32gui.SendMessage(hwnd, win32con.CB_SETCURSEL, item, 0) win32gui.SendMessage(hwnd, win32con.WM_SETFOCUS, 0, 0 ) time.sleep(1) tmp=win32gui.GetWindowRect(hwnd) #print 'selectComboboxItemThird',tmp ''' win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0]+1, tmp[1]+1) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0]+1, tmp[1]+1) ''' win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, 13, 0 ) time.sleep(0.1) win32gui.SendMessage(hwnd, win32con.WM_KEYUP, 13, 0 ) time.sleep(0.1) #_sendNotifyMessage(hwnd, win32con.CBN_SELCHANGE) except TypeError: # Item is a string - find the index, and use that items = getComboboxItems(hwnd) itemIndex = items.index(item) selectComboboxItem(hwnd, itemIndex)
def Mouse_LB_Double(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) print 'Mouse_RB str_app,hwnd ',str_app,hwnd if hwnd < 1: hwnd = self.find_main_window(str_app) win32api.SetCursorPos(tmp) time.sleep(1) #win32api.SetDoubleCIckTime() win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.005) return True
def Mouse_LB_D(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) print 'Mouse_RB str_app,hwnd ',str_app,hwnd if hwnd < 1: hwnd = self.find_main_window(str_app) win32api.SetCursorPos(tmp) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True
def Mouse_LB(self,str_app,lb_dx,lb_dy,Flag='1'): print "*********Mouse_LB function**********" time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) #win32gui.ShowWindow(hwnd, 0) win32api.SetCursorPos(tmp) print 'Mouse_LB tmp =',tmp if Flag == '1': time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True
def CFQS(): try: e = win32gui.GetWindowRect (win32gui.FindWindowEx(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????'),None,'#32770','???????'),None,'SysListView32','CFQS')) win32api.SetCursorPos([e[0]+50,e[1]+25]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #????
def button_event(content): key_table = {'BACKSPACE':8, 'TAB':9, 'TABLE':9, 'CLEAR':12, 'ENTER':13, 'SHIFT':16, 'CTRL':17, 'CONTROL':17, 'ALT':18, 'ALTER':18, 'PAUSE':19, 'BREAK':19, 'CAPSLK':20, 'CAPSLOCK':20, 'ESC':27, 'SPACE':32, 'SPACEBAR':32, 'PGUP':33, 'PAGEUP':33, 'PGDN':34, 'PAGEDOWN':34, 'END':35, 'HOME':36, 'LEFT':37, 'UP':38, 'RIGHT':39, 'DOWN':40, 'SELECT':41, 'PRTSC':42, 'PRINTSCREEN':42, 'SYSRQ':42, 'SYSTEMREQUEST':42, 'EXECUTE':43, 'SNAPSHOT':44, 'INSERT':45, 'DELETE':46, 'HELP':47, 'WIN':91, 'WINDOWS':91, 'F1':112, 'F2':113, 'F3':114, 'F4':115, 'F5':116, 'F6':117, 'F7':118, 'F8':119, 'F9':120, 'F10':121, 'F11':122, 'F12':123, 'F13':124, 'F14':125, 'F15':126, 'F16':127, 'NMLK':144, 'NUMLK':144, 'NUMLOCK':144, 'SCRLK':145, 'SCROLLLOCK':145, 'LEFTCLICK':999, 'RIGHTCLICK':1000} unrecognized = '' key_values = [] keys = content.split('+') for key in keys: raw_key = key key = key.strip().replace(' ','').upper() if key in key_table: key_values.append(key_table.get(key)) elif len(key) == 1: key_values.append(ord(key)) else: if key != '': unrecognized = raw_key for key_value in key_values: if key_value == 999: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) elif key_value == 1000: win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) else: win32api.keybd_event(key_value, 0, 0, 0) time.sleep(1) for i in range(len(key_values)-1, -1, -1): if key_value == 999: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) elif key_value == 1000: win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) else: win32api.keybd_event(key_values[i], 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(1) return unrecognized #??????????
def stop_marking(self): if self.__is_marking: self.__is_marking = False win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) return True return False
def __toggle_marking(self): if self.__is_marking: self.stop_marking() else: self.__is_marking = True win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
def _input_left_mouse(self, x, y): left, top, right, bottom = self.rect width, height = right - left, bottom - top if x < 0 or x > width or y < 0 or y > height: return win32gui.SetForegroundWindow(self.hwnd) pos = win32gui.GetCursorPos() win32api.SetCursorPos((left+x, top+y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.Sleep(100) #ms win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) win32api.Sleep(100) #ms # win32api.SetCursorPos(pos)
def mouse_click(): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def clickWindow(hwnd, offset): left, top, right, bottom = win32gui.GetWindowRect(hwnd) # print('left, top, right, bottom', left, top, right, bottom) win32api.SetCursorPos([left + offset, (bottom - top) // 2 + top]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) time.sleep(0.2) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) time.sleep(0.2)
def MouseLDown(cls): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) time.sleep(GLB_SLEEP_TIME)
def MouseLUp(cls): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) time.sleep(GLB_SLEEP_TIME)
def MouseRDown(cls): win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) time.sleep(GLB_SLEEP_TIME)
def MouseRUp(cls): win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) time.sleep(GLB_SLEEP_TIME)
def MouseMove(cls, x, y): sw = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) sh = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) nx = int(x * 65535 / sw) ny = int(y * 65535 / sh) win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE, nx, ny, 0, 0) time.sleep(GLB_SLEEP_TIME)
def RClick(cls): win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN|win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) time.sleep(GLB_SLEEP_TIME)
def _mouse_click(x, y): win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
def click(self, x, y): win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def Mouseclick_lb(self,tmp=(220,230)): win32api.SetCursorPos(tmp) print 'Mouseclick_lb tmp:',tmp time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.1) return True
def click_CurrentPlace(self,tmp=(259, 178)): win32api.SetCursorPos(tmp) self.sleep('1') win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) self.sleep('0.05') win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) self.sleep('0.05')
def Mouse_LB_D(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) #msg = 'Mouse_LB str_app,hwnd '+str_app+' '+ str(hwnd) #log_print(msg) if hwnd > 0: win32api.SetCursorPos(tmp) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True return False
def FX_MouseLClick(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def FX_MouseRClick(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
def FX_MouseLDown(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, int(x), int(y), 0, 0)
def FX_MouseLUp(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, int(x), int(y), 0, 0)
def FX_MouseScroll(i): win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, i,0)
def release(self, x, y, button=1): buttonAction = 2 ** ((2 * button)) self.move(x, y) win32api.mouse_event(buttonAction, x, y)
def scroll(self, vertical=None, horizontal=None, depth=None): #Windows supports only vertical and horizontal scrolling if depth is not None: raise ScrollSupportError('PyMouse cannot support depth-scrolling \ in Windows. This feature is only available on Mac.') #Execute vertical then horizontal scrolling events if vertical is not None: vertical = int(vertical) if vertical == 0: # Do nothing with 0 distance pass elif vertical > 0: # Scroll up if positive for _ in range(vertical): win32api.mouse_event(0x0800, 0, 0, 120, 0) else: # Scroll down if negative for _ in range(abs(vertical)): win32api.mouse_event(0x0800, 0, 0, -120, 0) if horizontal is not None: horizontal = int(horizontal) if horizontal == 0: # Do nothing with 0 distance pass elif horizontal > 0: # Scroll right if positive for _ in range(horizontal): win32api.mouse_event(0x01000, 0, 0, 120, 0) else: # Scroll left if negative for _ in range(abs(horizontal)): win32api.mouse_event(0x01000, 0, 0, -120, 0)
def mouse_click(x=None, y=None): if not x is None and not y is None: mouse_move(x, y) time.sleep(random.random()) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def Click_Element_None_Mouse(Element,Expand=None,Invoke=None,Select=None,Toggle=None): try: x = (int)(Element.Current.BoundingRectangle.Right - Element.Current.BoundingRectangle.Width / 2); y = (int)(Element.Current.BoundingRectangle.Bottom - Element.Current.BoundingRectangle.Height / 2); win32api.SetCursorPos((x,y)) print "clicking your element" patter_list = Element.GetSupportedPatterns() for each in patter_list: pattern_name = Automation.PatternName(each) if pattern_name == "ExpandCollapse": if Expand==True: #check to see if its expanded, if expanded, then do nothing... if not, expand it status = Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Current.ExpandCollapseState if status == 0: Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Expand() elif status == 1: print "Already Expanded" elif Expand == False: #check to see if its Collapsed, if Collapsed, then do nothing... if not, Collapse it status = Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Current.ExpandCollapseState if status == 1: Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Collapse() elif status == 0: print "Already Collapsed" elif pattern_name == "Invoke": if Invoke==True: print "invoking the button: %s" %Element.Current.Name time.sleep(2) Element.GetCurrentPattern(InvokePattern.Pattern).Invoke() elif pattern_name == "SelectionItem": Element.GetCurrentPattern(SelectionItemPattern.Pattern).Select() elif pattern_name == "Toggle": Element.GetCurrentPattern(TogglePattern.Pattern).Toggle() else: #x = int (Element.Current.BoundingRectangle.X) #y = int (Element.Current.BoundingRectangle.Y) x = (int)(Element.Current.BoundingRectangle.Right - Element.Current.BoundingRectangle.Width / 2); y = (int)(Element.Current.BoundingRectangle.Bottom - Element.Current.BoundingRectangle.Height / 2); win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) except Exception, e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print ((str(exc_type).replace("type ", "Error Type: ")) + ";" + "Error Message: " + str(exc_obj) +";" + "File Name: " + fname + ";" + "Line: "+ str(exc_tb.tb_lineno))