Performing a double click using CGEventCreateMouseEvent()

浪子不回头ぞ 提交于 2019-11-27 18:51:14
phoebus

Look into CGEventSetIntegerValueField(event, kCGMouseEventClickState, clickCount). Also, even after setting the clickCount to 2, you may have to perform 2 events in some cases, for legacy apps.

So basically:

  1. Create event
  2. Set click count to 2
  3. Set event type to mousedown and send
  4. Set event type to mouseup and send
  5. Repeat 3 and 4

Edit:

void doubleClick(int clickCount) {
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);  
    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, clickCount);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseDown);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp); 
    CGEventPost(kCGHIDEventTap, theEvent); 
    CFRelease(theEvent); 
}

This is sample code to simulate a double click.

CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(x, y), 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); 

CFRelease(theEvent); 

Notice that CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);goes after the first set of down,up clicks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!