查看 Mac 是否支持 Force Touch
Find out if Mac is Force Touch - capable
是否可以查明 Mac 是否支持 Force Touch - 通过内置触控板(如新的 MacBook)或蓝牙设备(如 Magic Trackpad 2) ?
如果 Mac 支持 Force Touch,我想提供特定于 Force Touch 的首选项,但如果 Force Touch 不可用,我不显示(或禁用)这些首选项。
在分隔符之后的部分,您可以在此处链接的图片中看到我想到的选项。 (抱歉,嵌入图片本身不起作用)。
所以,不显示首选项不会限制没有强制触摸的用户,它只是让拥有它的用户配置它应该如何工作,而那些设置对没有的用户没有用有了。
有办法实现吗?
谢谢你和亲切的问候,
马特
编辑:它在 Objective-C.
我想通了:
+ (BOOL)isForceTouchCapable
{
if (![[self class] isAtLeastElCapitan])
return NO;
io_iterator_t iterator;
//get default HIDDevice dictionary
CFMutableDictionaryRef mDict = IOServiceMatching(kIOHIDDeviceKey);
//add manufacturer "Apple Inc." to dict
CFDictionaryAddValue(mDict, CFSTR(kIOHIDManufacturerKey), CFSTR("Apple Inc."));
//get matching services, depending on dict
IOReturn ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, mDict, &iterator);
BOOL result = YES;
if (ioReturnValue != kIOReturnSuccess)
NSLog(@"error getting matching services for force touch devices");
else
{
//recursively go through each device found and its children and grandchildren, etc.
result = [[self class] _containsForceTouchDevice:iterator];
IOObjectRelease(iterator);
}
return result;
}
+ (BOOL)_containsForceTouchDevice:(io_iterator_t)iterator
{
io_object_t object = 0;
BOOL success = NO;
while ((object = IOIteratorNext(iterator)))
{
CFMutableDictionaryRef result = NULL;
kern_return_t state = IORegistryEntryCreateCFProperties(object, &result, kCFAllocatorDefault, 0);
if (state == KERN_SUCCESS && result != NULL)
{
if (CFDictionaryContainsKey(result, CFSTR("DefaultMultitouchProperties")))
{
CFDictionaryRef dict = CFDictionaryGetValue(result, CFSTR("DefaultMultitouchProperties"));
CFTypeRef val = NULL;
if (CFDictionaryGetValueIfPresent(dict, CFSTR("ForceSupported"), &val))
{
Boolean aBool = CFBooleanGetValue(val);
if (aBool) //supported
success = YES;
}
}
}
if (result != NULL)
CFRelease(result);
if (success)
{
IOObjectRelease(object);
break;
} else
{
io_iterator_t childIterator = 0;
kern_return_t err = IORegistryEntryGetChildIterator(object, kIOServicePlane, &childIterator);
if (!err)
{
success = [[self class] _containsForceTouchDevice:childIterator];
IOObjectRelease(childIterator);
} else
success = NO;
IOObjectRelease(object);
}
}
return success;
}
只需调用 + (BOOL)isForceTouchCapable,如果 Force Touch 设备可用(Magic Trackpad 2 或内置 Force-touch-trackpad),它会 return YES,如果没有,则为 NO .
对于那些对它是如何发生感兴趣的人,我用一个示例项目写了它 on my blog。
是否可以查明 Mac 是否支持 Force Touch - 通过内置触控板(如新的 MacBook)或蓝牙设备(如 Magic Trackpad 2) ?
如果 Mac 支持 Force Touch,我想提供特定于 Force Touch 的首选项,但如果 Force Touch 不可用,我不显示(或禁用)这些首选项。
在分隔符之后的部分,您可以在此处链接的图片中看到我想到的选项。 (抱歉,嵌入图片本身不起作用)。
所以,不显示首选项不会限制没有强制触摸的用户,它只是让拥有它的用户配置它应该如何工作,而那些设置对没有的用户没有用有了。
有办法实现吗?
谢谢你和亲切的问候, 马特
编辑:它在 Objective-C.
我想通了:
+ (BOOL)isForceTouchCapable
{
if (![[self class] isAtLeastElCapitan])
return NO;
io_iterator_t iterator;
//get default HIDDevice dictionary
CFMutableDictionaryRef mDict = IOServiceMatching(kIOHIDDeviceKey);
//add manufacturer "Apple Inc." to dict
CFDictionaryAddValue(mDict, CFSTR(kIOHIDManufacturerKey), CFSTR("Apple Inc."));
//get matching services, depending on dict
IOReturn ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, mDict, &iterator);
BOOL result = YES;
if (ioReturnValue != kIOReturnSuccess)
NSLog(@"error getting matching services for force touch devices");
else
{
//recursively go through each device found and its children and grandchildren, etc.
result = [[self class] _containsForceTouchDevice:iterator];
IOObjectRelease(iterator);
}
return result;
}
+ (BOOL)_containsForceTouchDevice:(io_iterator_t)iterator
{
io_object_t object = 0;
BOOL success = NO;
while ((object = IOIteratorNext(iterator)))
{
CFMutableDictionaryRef result = NULL;
kern_return_t state = IORegistryEntryCreateCFProperties(object, &result, kCFAllocatorDefault, 0);
if (state == KERN_SUCCESS && result != NULL)
{
if (CFDictionaryContainsKey(result, CFSTR("DefaultMultitouchProperties")))
{
CFDictionaryRef dict = CFDictionaryGetValue(result, CFSTR("DefaultMultitouchProperties"));
CFTypeRef val = NULL;
if (CFDictionaryGetValueIfPresent(dict, CFSTR("ForceSupported"), &val))
{
Boolean aBool = CFBooleanGetValue(val);
if (aBool) //supported
success = YES;
}
}
}
if (result != NULL)
CFRelease(result);
if (success)
{
IOObjectRelease(object);
break;
} else
{
io_iterator_t childIterator = 0;
kern_return_t err = IORegistryEntryGetChildIterator(object, kIOServicePlane, &childIterator);
if (!err)
{
success = [[self class] _containsForceTouchDevice:childIterator];
IOObjectRelease(childIterator);
} else
success = NO;
IOObjectRelease(object);
}
}
return success;
}
只需调用 + (BOOL)isForceTouchCapable,如果 Force Touch 设备可用(Magic Trackpad 2 或内置 Force-touch-trackpad),它会 return YES,如果没有,则为 NO .
对于那些对它是如何发生感兴趣的人,我用一个示例项目写了它 on my blog。