如何读取石英事件中的鼠标增量?

How to read mouse delta in Quartz events?

在 Quartz 应用程序中,我试图在屏幕上冻结鼠标 pointer 但继续从用户注册鼠标 mouvements .我找到了如何冻结指针:

CGAssociateMouseAndMouseCursorPosition(false);

我正在关注 documentation 但不知道如何获取和读取 包含鼠标增量(X 和 Y 的变化)数据的事件

我找到了解决办法。我现在的鼠标移动是这样的:

CGEventMask eventMask = CGEventMaskBit(kCGEventMouseMoved);
CFMachPortRef tap = CGEventTapCreate(   kCGAnnotatedSessionEventTap, 
                                        kCGHeadInsertEventTap, 
                                        kCGEventTapOptionListenOnly, 
                                        eventMask, 
                                        eventOccurred, 
                                        NULL);
CFRunLoopSourceRef rl = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), rl, kCFRunLoopCommonModes);
CGEventTapEnable(tap, true);

回调在哪里:

CGEventRef eventOccurred(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) {
    if (type == kCGEventMouseMoved) {
        d_x =  CGEventGetIntegerValueField(event, kCGMouseEventDeltaX);
        d_y =  CGEventGetIntegerValueField(event, kCGMouseEventDeltaY);
    }
    return event;
}

注意:d_xd_y是全局定义的int,用于存储每个事件之间的鼠标移动。