3

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.

1 Answers1

1

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

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • Wow this is great! Do you happen to know the difference between kIOHIDPointerAccelerationKey, kIOHIDMouseAccelerationType and kIOHIDPointerAccelerationType? – Miguel Barros Jul 31 '19 at 15:32
  • I ultimately wond up doing it in a simpler way, but I have a feeling yours is more correct: – Miguel Barros Jul 31 '19 at 15:39
  • For mouse acceleration, you definitely need the key that I recommend. – jvarela Jul 31 '19 at 15:39
  • You can check the other available keys in IOKit/hidsystem/IOHIDParameter.h header that I included in this code, where you can find other keys related to acceleration, including the acceleration setting for trackpad. – jvarela Jul 31 '19 at 15:40
  • io_service_t service = IORegistryEntryFromPath(kIOMasterPortDefault, kIOServicePlane ":/IOResources/IOHIDSystem"); auto parameters = (CFDictionaryRef) IORegistryEntryCreateCFProperty(service, CFSTR(kIOHIDParametersKey), kCFAllocatorDefault, kNilOptions); CFNumberRef reference; reference = (CFNumberRef)CFDictionaryGetValue( parameters, CFSTR(kIOHIDMouseAccelerationType)); int64_t acceleration; CFNumberGetValue(reference, kCFNumberSInt64Type, &acceleration)) – Miguel Barros Jul 31 '19 at 15:42
  • when I use that key and then change the mouse tracking speed using system preferences the value doesn't change so I assume one is used for mouse tracking speed – Miguel Barros Jul 31 '19 at 15:44
  • You are right, the right key is kIOHIDMouseAccelerationType. I was induced into error by the original code. I will edit my answer. – jvarela Jul 31 '19 at 15:48
  • Your code is somewhat dangerous, because you are not checking the success of your calls and you are not releasing the memory that you are allocating. I guess you just showed me a minimal case. – jvarela Jul 31 '19 at 15:52
  • Hi your solution seems to crash my pc entirely after a while any idea why? – Miguel Barros Aug 13 '19 at 10:18
  • I had no such problem. Are you sure you are not over releasing a pointer? – jvarela Aug 13 '19 at 10:32
  • I got it since you return imediately and the process is killed it doesn't crash but still it's best to call IOServiceClose once you are done, it caused crashes for me after a while of running because I opened multiples services everytime I recalculated the mouse DPI – Miguel Barros Aug 13 '19 at 10:34
  • Can’t you catch the crash in a debugger? Can you go to the Console.app in Utilities inside the Applications folder and look for any crash logs of your app and see where your app crashes? – jvarela Aug 13 '19 at 10:37
  • It doesn't much crash as it just consumes way to much resources and the whole computer stops responding – Miguel Barros Aug 13 '19 at 12:54
  • Again, go to Console.app, click on System Reports and look for reports with your app. – jvarela Aug 13 '19 at 12:59