Simulate mouse on Mac

扶醉桌前 提交于 2019-11-30 15:58:21
Nick Moore

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.

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