views:

216

answers:

1
+1  A: 

CGDisplayMoveCursorToPoint() only moves the image of the cursor, it does not generate any events. You should create and post mouse events of type kCGEventMouseMoved to simulate moving the mouse. Your own method would do it:

[self postMouseEventWithButton:0 withType:kCGEventMouseMoved andPoint:point];

For clicks, you are already doing it the right way, I think. One thing you should also do is set the click count properly on both the mouse down and mouse up events, like so:

CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);

... because some applications need it.

(See also http://stackoverflow.com/questions/2369806/simulating-mouse-clicks-on-mac-os-x-does-not-work-for-some-applications/)

If your code doesn't work, I'm not sure why; it looks OK to me. Try posting to kCGSessionEventTap instead of kCGHIDEventTap and see if it helps. Also, you don't need the CGEventSetType() call since the type is already set in the creation call.

invariant
Thanks so I wrote : -(void)postMouseEventWithButton:(CGMouseButton)b withType:(CGEventType)t andPoint:(CGPoint)p{ CGEventRef theEvent = CGEventCreateMouseEvent(NULL, t, p, b); CGEventSetType(theEvent, t); CGEventPost(kCGHIDEventTap, theEvent); CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 1); CFRelease(theEvent);}but nothing has changed. And I don't understand why CGDisplayMoveCursorToPoint is working bad (dock problem etc). Have you got an idea?
Pierre
I edited my answer to add more info, hope it helps.
invariant