3

我想在 OSX 上获得鼠标跟踪速度我在文档中找到的所有方法,例如 IOHIDGetAccelerationWithKey 已被弃用。有没有办法做到这一点?我还没有找到一个不使用已弃用方法的示例,并且文档还没有使用任何相关的东西。

4

1 回答 1

1

就在这里。试试这个代码,它只使用未弃用的 API:

#include <iostream>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hidsystem/event_status_driver.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>

int main(int argc, const char * argv[]) {
    NXEventHandle handle = MACH_PORT_NULL;
    kern_return_t kr;
    io_service_t service = MACH_PORT_NULL;
    mach_port_t masterPort;
    CFTypeRef typeRef = NULL;
    CFNumberRef number = NULL;
    unsigned int acceleration;

    do {
        kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
        if (kr != KERN_SUCCESS) break;
        service = IORegistryEntryFromPath(masterPort, kIOServicePlane ":/IOResources/IOHIDSystem");
        if (!service) break;
        kr = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &handle);
        if (kr != KERN_SUCCESS) break;
        kr = IOHIDCopyCFTypeParameter(handle, CFSTR(kIOHIDMouseAccelerationType), &typeRef);
        if (kr != KERN_SUCCESS) break;
        number = (CFNumberRef)typeRef;
        CFNumberGetValue(number, kCFNumberSInt32Type, &acceleration);
        std::cout << "Acceleration is " << acceleration << std::endl;
        CFRelease(typeRef);
        IOObjectRelease( service );
    } while (false);

    return 0;
}

这是基于您可以在最新版本的 Darwin 中找到的代码,您可以在此处找到:

https://opensource.apple.com/source/IOKitUser/IOKitUser-1483.220.15/hidsystem.subproj/IOEventStatusAPI.c.auto.html

于 2019-07-31T15:21:35.917 回答