当前位置: 首页 > 工具软件 > PyObjC > 使用案例 >

pyobjc实现鼠标事件模拟(Mac)

壤驷向明
2023-12-01

既然pyobjc能够使用cocoa提供的这么多库函数,那对我们有没有一些实际的作用呢?联想到在自动化测试中,appscript有些操作目前还是无法实现的,例如:

无法实现点击屏幕任意坐标(appscript中未找到类似的api)

无法双击某个控件(通过appscript两次调用控件的click方法是不生效的)

无法实现控件上右键……

现在,这些appscript无法完成的操作,通过pyobjc又找到了另一条实现的途径,我们可以借助pyobjc,来构造并触发我们需要的鼠标事件,cocoa的 CoreGraphics库中就包含了构造所有鼠标事件的方法,在苹果开发者网站上了解了这些函数的使用方式后,实现一个fakemouse,能否完成的操作有:点击屏幕任意坐标、在任意坐标双击、在任意坐标鼠标右键、移动鼠标至某个坐标、鼠标拖拽等。具体实现代码如下:

 

#!/usr/bin/python
'''
Created on 2013-8-30

@author: maricoliu
'''
import sys
import time
from Quartz.CoreGraphics import *  

def _mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)
    
def mouseMove(posx, posy):
    _mouseEvent(kCGEventMouseMoved, posx, posy)
    
def mouseClickDown(posx, posy):
    _mouseEvent(kCGEventLeftMouseDown, posx, posy)
    
def mouseClickUp(posx, posy):
    _mouseEvent(kCGEventLeftMouseUp, posx, posy)
    
def mouseDrag(posx, posy):
    _mouseEvent(kCGEventLeftMouseDragged, posx, posy)
    
def mouseClick(posx, posy):
    '''perform a left click'''
    _mouseEvent(kCGEventLeftMouseDown, posx, posy)
    _mouseEvent(kCGEventLeftMouseUp, posx, posy)
    
def mouseRightClick(posx, posy):
    theEvent = CGEventCreateMouseEvent(None, kCGEventRightMouseDown, (posx,posy), kCGMouseButtonRight)
    CGEventPost(kCGHIDEventTap, theEvent)
    theEvent2 = CGEventCreateMouseEvent(None, kCGEventRightMouseUp, (posx,posy), kCGMouseButtonRight)
    CGEventPost(kCGHIDEventTap, theEvent2)

def mouseDoubleClick(posx, posy):
    '''perfrom a double left click'''
    theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx,posy), kCGMouseButtonLeft);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp);  
    CGEventPost(kCGHIDEventTap, theEvent); 
    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);   
    CGEventSetType(theEvent, kCGEventLeftMouseDown);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp); 
    CGEventPost(kCGHIDEventTap, theEvent);

def mouseScroll(movement=30, direction=1):  
    '''
    @param movement: lines to scroll, Integer 



    @param direction: scroll up or scroll down, 1:scroll up, -1:scroll down 
    '''      
    for i in range(movement):
        theEvent = CGEventCreateScrollWheelEvent(None, kCGScrollEventUnitLine, 1, direction)
        CGEventPost(kCGHIDEventTap, theEvent)
        time.sleep(0.02)       
    #CGPostScrollWheelEvent(1, 5)


if __name__ == '__main__':
    ourEvent = CGEventCreate(None);
    currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
    time.sleep(1);

    for i in range(20):
        mouseScroll(40, 1)
        mouseScroll(40, -1)
    
    # mouseclick(1610, 215)
    # mousedoubleclick(1697, 561)
    time.sleep(1);
    #mouseMove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

 

 类似资料: