首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么CGWarpMouseCursorPosition会导致延迟?如果不是,那是什么呢?

为什么CGWarpMouseCursorPosition会导致延迟?如果不是,那是什么呢?
EN

Stack Overflow用户
提问于 2011-11-21 16:53:12
回答 3查看 3K关注 0票数 7

我这里有一些代码,把鼠标限制在屏幕上的一个区域,它工作得比较好,只有一个大问题。当鼠标沿着区域边缘运行时,移动并不干净/流畅,而是以一种非常剧烈的方式跳跃,我相信这可能是由于CGWarpMouseCursorPosition造成了每一个“翘曲”的延迟。

可以告诉任何人,是我代码中的什么东西导致了这种延迟,还是实际上是鼠标扭曲函数造成的。如果是鼠标扭曲函数,有什么方法可以让鼠标顺利地重新定位吗?,我在flash中做了同样的事情,它运行得非常完美,我知道这个循环不需要花费太多的时间来执行,因为它只运行了4或5次。

代码语言:javascript
运行
复制
CGEventRef 
mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {


    CGPoint point = CGEventGetLocation(event);

    float tX = point.x;
    float tY = point.y;

    if( tX <= 700 && tX >= 500 && tY <= 800 && tY >= 200){
        // target is inside O.K. area, do nothing
    }else{

    CGPoint target; 

    //point inside restricted region:
    float iX = 600; // inside x
    float iY = 500; // inside y

    // delta to midpoint between iX,iY and tX,tY
    float dX;
    float dY;

    float accuracy = .5; //accuracy to loop until reached

    do {
        dX = (tX-iX)/2;
        dY = (tY-iY)/2;

        if((tX-dX) <= 700 && (tX-dX) >= 500 && (tY-dY) <= 800 && (tY-dY) >= 200){
            iX += dX;
            iY += dY;
        } else {
            tX -= dX;
            tY -= dY;
        }

    } while (abs(dX)>accuracy || abs(dY)>accuracy);

        target = CGPointMake(roundf(tX), roundf(tY));
        CGWarpMouseCursorPosition(target);

    }



    return event;
}

int
main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    CFRunLoopSourceRef runLoopSource;
    CGEventMask event_mask;
    event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged);

    CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, NULL);

    if (!eventTap) {
        NSLog(@"Couldn't create event tap!");
        exit(1);
    }

    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

    CGEventTapEnable(eventTap, true);

    CFRunLoopRun();

    CFRelease(eventTap);
    CFRelease(runLoopSource);
    [pool release];

    exit(0);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-09 21:58:49

正如您所发现的,CGSetLocalEventsSuppressionInterval解决了您的问题。

然而,它在10.6岁时就被否决了。苹果公司的文档声明:

此功能不推荐用于一般用途,因为没有记录的特殊情况和不良副作用。建议将此函数替换为CGEventSourceSetLocalEventsSuppressionInterval,它允许针对特定事件源调整抑制间隔,只影响使用该事件源发布的事件。

不幸的是,替代的CGEventSourceSetLocalEventsSuppressionInterval不适用于CGWarpMouseCursorPosition移动。

相反,在翘曲之后立即使用CGAssociateMouseAndMouseCursorPosition(true)

代码语言:javascript
运行
复制
CGPoint warpPoint = CGPointMake(42, 42);
CGWarpMouseCursorPosition(warpPoint);
CGAssociateMouseAndMouseCursorPosition(true);

文档没有提到这种行为,但它似乎在翘曲之后取消了抑制间隔。

票数 7
EN

Stack Overflow用户

发布于 2018-08-10 13:14:13

我可以让CGEventSourceSetLocalEventsSuppressionInterval以下列方式使用CGWarpMouseCursorPosition

代码语言:javascript
运行
复制
auto eventSourceRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsSuppressionInterval(eventSourceRef, 0);

这在CGWarpMouseCursorPosition中很好(不被反对),而CGAssociateMouseAndMouseCursorPosition(true)对我没有任何帮助。

票数 4
EN

Stack Overflow用户

发布于 2011-11-24 03:36:56

您可能正在寻找CGSetLocalEventsSuppressionInterval(),这是一个在10.6岁时被废弃的方法..。但在10.7中,它仍然有效。

http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/c/func/CGSetLocalEventsSuppressionInterval

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8215413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档