简体   繁体   中英

Get Mouse Tracking Speed with C/C++ on Mac OS

I want to get the Mouse Tracking Speed on OSX all the methods I've found on the documentation, like IOHIDGetAccelerationWithKey have been deprecated. Is there any way to do this? I haven't found an example that doesn't use the deprecated methods and the documentation hasn't wielded anything relevant yet.

Yes, there is. Try this code, which uses only non-deprecated APIs:

#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;
}

This is based on code you can find in the latest release of Darwin, which you can find here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM