在Mac上模拟鼠标 [英] Simulate mouse on Mac

查看:487
本文介绍了在Mac上模拟鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

-(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()仅移动图像对于游标,它不会生成任何事件。您应该创建并发布类型为 kCGEventMouseMoved 的鼠标事件,以模拟移动鼠标。你自己的方法会这样做:

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.

(另见在Mac OS X上模拟鼠标点击对某些应用程序不起作用

如果你的代码不起作用,我不确定为什么;它对我来说很好看。尝试发布到 kCGSessionEventTap 而不是 kCGHIDEventTap ,看看是否有帮助。此外,您不需要 CGEventSetType()调用,因为该类型已在创建调用中设置。

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.

这篇关于在Mac上模拟鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆