是否 possible/safe 以编程方式更改 OSX 鼠标系统首选项设置,例如 Tracking Speed?

Is it possible/safe to change the OSX mouse system preferences settings such as Tracking Speed programmatically?

我做了一些研究,看起来它存储在 IORegistry 中 在“:/IOResources/HIDSystem”下为 "HIDMouseAcceleration".

可以使用 IORegistry API 从用户程序设置这些吗?

还有其他方法可以以编程方式更改鼠标系统首选项设置吗?

我选择的编程语言是 C。该方法需要在 OS X v10.11+ 上工作。

谢谢。

是的,这是可能的。这是一些粗略的代码:

io_object_t hidSystemParametersConnection = IO_OBJECT_NULL;

// We're looking for a service of the IOHIDSystem class
CFMutableDictionaryRef classesToMatch = IOServiceMatching("IOHIDSystem");
if (!classesToMatch)
    /* handle failure */;

// The following call implicitly releases classesToMatch
io_iterator_t matchingServicesIterator = IO_OBJECT_NULL;
IOReturn ret = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServicesIterator);
if (ret != kIOReturnSuccess)
    /* handle failure */;

io_object_t service;
while ((service = IOIteratorNext(matchingServicesIterator)))
{
    // Open the parameters connection to the HIDSystem service
    ret = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &hidSystemParametersConnection);
    IOObjectRelease(service);

    if (ret == kIOReturnSuccess && hidSystemParametersConnection != IO_OBJECT_NULL)
        break;
}

IOObjectRelease(matchingServicesIterator);

CFTypeRef value;
ret = IOHIDCopyCFTypeParameter(hidSystemParametersConnection, CFSTR(kIOHIDPointerAccelerationKey), &value);
if (ret != kIOReturnSuccess || !value)
    /* handle failure */;

if (CFGetTypeID(value) != CFNumberGetTypeID())
{
    CFRelease(value);
    /* handle wrong type */
}

NSNumber* accel = CFBridgingRelease(value);
double newAccel = accel.doubleValue / 2;

ret = IOHIDSetCFTypeParameter(hidSystemParametersConnection, CFSTR(kIOHIDPointerAccelerationKey), (__bridge CFTypeRef)@(newAccel));
if (ret != kIOReturnSuccess)
    /* handle failure */;

IOServiceClose(hidSystemParametersConnection);

在 IOKit.framework/Headers/hid/IOHIDProperties.h 和 IOKit.framework/Headers/hidsystem/IOHIDParameter.h 中定义并简要记录了各种参数键。我会尽可能多地在“系统偏好设置”中使用不同的硬件和配置进行彻底测试,以准确了解哪些参数相关以及它们的值意味着什么。