如何在系统偏好设置中打开Voice Over?

时间:2009-01-27 09:04:34

标签: cocoa macos macos-carbon assistive

是否有办法(理想情况下向后兼容Mac OS X 10.3)判断“系统偏好设置”中是否激活了“Voice Over”?

4 个答案:

答案 0 :(得分:4)

这似乎存储在Universal Access的首选项文件中。应用程序标识符为“com.apple.universalaccess”,包含VoiceOver是打开还是关闭标志的键是“voiceOverOnOffKey”。您应该能够使用CFPreferences API检索它,如下所示:

CFBooleanRef flag = CFPreferencesCopyAppValue(CFSTR("voiceOverOnOffKey"), CFSTR("com.apple.universalaccess"));

答案 1 :(得分:1)

基于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和可访问性是非常重要的主题,令人遗憾的是,缺少Apple文档,尤其是针对macOS的Apple文档,使开发人员难以正确实施它。

答案 2 :(得分:1)

如果有人有相同的问题,很高兴知道,现在可以通过方便的界面访问“旁白”状态:

NSWorkspace.shared.isVoiceOverEnabled

答案 3 :(得分:0)

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])