Performing a double click using CGEventCreateMouse

2019-01-13 14:12发布

I'm using the following code to simulate a click of the mouse:

void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point) 
{
 CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
 CGEventSetType(theEvent, type);
 CGEventPost(kCGHIDEventTap, theEvent);
 CFRelease(theEvent);
}

void LeftClick(const CGPoint point) 
{
 PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
 NSLog(@"Click!");
 PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
 PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}

I can use basically the same code to do a control-click (right click) by changing:

kCGEventLeftMouseDown

kCGEventLeftMouseUp

kCGMouseButtonLeft

to their respective "Right" events. The function looks something like:

void RightClick(const CGPoint point) 
{
 PostMouseEvent(kCGMouseButtonRight, kCGEventMouseMoved, point);
 NSLog(@"Click Right");
 PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseDown, point);
 PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseUp, point);
}

But, how about a double click? I tried sending 2 leftclicks and calling PostMouseEvent() twice in a row but no luck. How do you perform a double click?

thanks!

2条回答
时光不老,我们不散
2楼-- · 2019-01-13 14:33

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.

查看更多
贼婆χ
3楼-- · 2019-01-13 14:42

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); 
}
查看更多
登录 后发表回答