有没有办法,理想情况下向后兼容Mac OS X 10.3,以判断在系统偏好设置中是否激活了“Voice Over”?
这似乎存储在Universal Access的首选项文件中。应用程序标识符为“com.apple.universalaccess”,包含VoiceOver是打开还是关闭标志的键是“voiceOverOnOffKey”。您应该能够使用CFPreferences API检索此内容,如下所示:
CFBooleanRef flag = CFPreferencesCopyAppValue(CFSTR("voiceOverOnOffKey"), CFSTR("com.apple.universalaccess"));
根据Petes的优秀答案,我创建了这个Swift 4.2解决方案,我发现它更容易阅读。我也认为在这种情况下使用计算属性而不是函数更方便。
var hasVoiceOverActivated: Bool {
let key = "voiceOverOnOffKey" as CFString
let id = "com.apple.universalaccess" as CFString
if let voiceOverActivated = CFPreferencesCopyAppValue(key, id) as? Bool {
return voiceOverActivated
}
return false
}
VoiceOver和Accessibility一般都是非常重要的主题,令人遗憾的是,缺乏Apples文档特别是对于macOS来说,开发人员很难正确地实现它。
如果有人有同样的问题,可以很高兴地知道,Voice Over状态现在可以通过方便的界面访问:
NSWorkspace.shared.isVoiceOverEnabled
Swift 4中的解决方案如下:
func NSIsVoiceOverRunning() -> Bool {
if let flag = CFPreferencesCopyAppValue("voiceOverOnOffKey" as CFString, "com.apple.universalaccess" as CFString) {
if let voiceOverOn = flag as? Bool {
return voiceOverOn
}
}
return false
}
此外,要在macOS上使用VoiceOver发布文本通知,请执行以下操作:
let message = "Hello, World!"
NSAccessibilityPostNotificationWithUserInfo(NSApp.mainWindow!,
NSAccessibilityNotificationName.announcementRequested,
[NSAccessibilityNotificationUserInfoKey.announcement: message,
NSAccessibilityNotificationUserInfoKey.priority:
NSAccessibilityPriorityLevel.high.rawValue])