在 Mac 上模拟鼠标

发布于 2024-08-23 05:30:12 字数 886 浏览 2 评论 0原文

我的 iPhone 上有一个虚拟触控板,用于移动我正在使用的鼠标:


CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, CGPointMake(((float)aD.msg)+location.x, ((float)aD.msg2)+location.y));

它运行良好,但这不是真正的鼠标,因为当我将鼠标放在隐藏的扩展坞上时,这个鼠标不会自行显示。我不明白为什么。 此外,我尝试用以下方法模拟鼠标点击:


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(事件); }

这是好方法吗?感谢您的帮助 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-08-30 05:30:12

CGDisplayMoveCursorToPoint() 仅移动光标的图像,它不会生成任何事件。您应该创建并发布 kCGEventMouseMoved 类型的鼠标事件来模拟移动鼠标。你自己的方法可以做到这一点:

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

对于点击,我认为你已经以正确的方式做到了。您还应该做的一件事是正确设置鼠标按下和鼠标松开事件的点击计数,如下所示:

CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);

...因为某些应用程序需要它。

(另请参阅模拟鼠标Mac OS X 上的点击不适用于某些应用程序

如果您的代码不起作用,我不确定为什么;我觉得还可以。尝试发布到 kCGSessionEventTap 而不是 kCGHIDEventTap 并查看是否有帮助。此外,您不需要 CGEventSetType() 调用,因为类型已在创建调用中设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文