开发者

Simulate mouse on Mac

I have a virtual trackpad on my iPhone and to move my mouse I'm using :


CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, CGPointMake(((float)aD.msg开发者_运维问答)+location.x, ((float)aD.msg2)+location.y));

It's working well but this not a real mouse because when I put my mouse on my hidden dock, this one doesn't display it self. I don't understand why. More over I tried to simulate mouse click with :


case MOUSECLICK:
[self postMouseEventWithButton:0 withType:kCGEventLeftMouseDown andPoint:CGEventGetLocation(CGEventCreate(NULL))];
[self postMouseEventWithButton:0 withType:kCGEventLeftMouseUp andPoint:CGEventGetLocation(CGEventCreate(NULL))];

// *********************

-(void)postMouseEventWithButton:(CGMouseButton)b withType:(CGEventType)t andPoint:(CGPoint)p { CGEventRef theEvent = CGEventCreateMouseEvent(NULL, t, p, b); CGEventSetType(theEvent, t); CGEventPost(kCGHIDEventTap, theEvent); CFRelease(theEvent); }

Is it the good method? Thanks for your help !


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜