views:

745

answers:

1

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!

+1  A: 

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() {  
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);  
    CGEventSetIntegerValueField(event, 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); 
}
phoebus
like: CGEventSetIntegerValueField(kCGMouseButtonLeft, kCGEventLeftMouseDown, 2)do I need to reset it to 1 after that?
Uri
You need to pass in a reference to the actual event object, in your case to "theEvent", as the first parameter. At that point, that event's click count is set to 2, and you want to leave it to 2 for all 4 CGEventPosts that you need to do (down, up, down, up).
phoebus
like this: CGEventSetIntegerValueField(kCGEventLeftMouseDown, kCGMouseEventClickState, 2);I get several warning...can you post a snippet? thanks!
Uri
See above edit.
phoebus
Thanks. One problem, once you perform the double click it won't click anywhere else. I pass as a parameter the current location of the mouse (point) but it still performs the double click on same point as before or the same application, regardless of whether it has focus. I'm assuming clickCount is 2, right? it's not declared anywhere.
Uri
I added CGEventSetType(theEvent, kCGEventLeftMouseDown); CGEventPost(kCGHIDEventTap, theEvent); becuase I thought it was missing but that wasn't the problem
Uri
I guess there is no answer...
Uri