【问题标题】:Simulate mouse on Mac在 Mac 上模拟鼠标
【发布时间】:2010-02-26 08:59:47
【问题描述】:

我的 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(theEvent); }

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

【问题讨论】:

    标签: iphone macos mouse mouseevent


    【解决方案1】:

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

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

    我认为,对于点击,您已经以正确的方式进行操作。您还应该做的一件事是正确设置鼠标按下和鼠标按下事件的点击次数,如下所示:

    CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);
    

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

    (另见Simulating mouse clicks on Mac OS X does not work for some applications

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

    【讨论】:

    • 谢谢,所以我写了:-(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(事件); } 但没有任何改变。而且我不明白为什么 CGDisplayMoveCursorToPoint 工作不好(码头问题等)。你有什么想法吗?
    猜你喜欢
    • 2011-12-30
    • 2011-03-25
    • 2011-04-02
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多