objective-c – 为什么CGWarpMouseCursorPosition导致延迟?如果不是,那是什么?

我这里的代码将鼠标限制在屏幕上的一个区域,它工作得相对较好,只有一个大问题.当沿着区域的边缘运行时,鼠标没有干净/平滑地移动,而是以非常波动的方式跳跃,我相信这可能是由于CGWarpMouseCursorPosition导致每次“扭曲”的延迟.

任何人都可以告诉我的代码中是否有导致此延迟的事情,或者它是否实际上是鼠标扭曲函数.如果是鼠标扭曲功能,有什么方法可以让鼠标顺利重新定位?我在闪存中做了同样的事情并且它完美无瑕地工作,我知道循环不仅花费了太多时间来执行它减慢了速度因为它只运行了4到5次.

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

解决方法

正如您所发现的,CGSetLocalEventsSuppressionInterval可以解决您的问题.

但是,从10.6开始,它已被弃用. Apple文档声明:

This function is not recommended for general use because of undocumented special cases and undesirable side effects. The recommended replacement for this function is CGEventSourceSetLocalEventsSuppressionInterval,which allows the suppression interval to be adjusted for a specific event source,affecting only events posted using that event source.

不幸的是,replacementCGEventSourceSetLocalEventsSuppressionInterval不适用于CGWarpMouseCursorPosition移动.

相反,在warp之后立即使用CGAssociateMouseAndMouseCursorPosition(true):

CGPoint warpPoint = CGPointMake(42,42);
CGWarpMouseCursorPosition(warpPoint);
CGAssociateMouseAndMouseCursorPosition(true);

文档没有提到这种行为,但似乎取消了warp之后的抑制间隔.

相关文章

/** C+⬑ * 默认成员函数 原来C++类中,有6个默认成员函数: 构造函数 析构函数 拷贝...
#pragma once // 1. 设计一个不能被拷贝的类/* 解析:拷贝只会放生在两个场景中:拷贝构造函数以及赋值运...
C类型转换 C语言:显式和隐式类型转换 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译...
//异常的概念/*抛出异常后必须要捕获,否则终止程序(到最外层后会交给main管理,main的行为就是终止) try...
#pragma once /*Smart pointer 智能指针;灵巧指针 智能指针三大件//1.RAII//2.像指针一样使用//3.拷贝问...
目录&lt;future&gt;future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程...