简体   繁体   中英

How can I tell if Voice Over is turned on in System Preferences?

有没有一种方法可以理想地向后兼容Mac OS X 10.3,以判断是否在“系统偏好设置”中激活了“ Voice Over”?

This appears to be stored in a preferences file for Universal Access. The app identifier is "com.apple.universalaccess" and the key containing the flag for whether VoiceOver is on or off is "voiceOverOnOffKey". You should be able to retrieve this using the CFPreferences API, something looking like:

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

Based on Petes excellent answer I've created this Swift 4.2 solution, which I find much easier to read. I also think it's more handy to use a computed property in this case instead of a function.

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 and Accessibility in general are very important topics and it is sad that the lack of Apples documentation especially for macOS makes it so hard for developers to implement it properly.

如果有人有相同的问题,最好先通过方便的界面访问“旁白”状态:

NSWorkspace.shared.isVoiceOverEnabled

Solution in Swift 4 is as follows:

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
}

Furthermore, to make a text announcement with VoiceOver on macOS, do the following:

let message = "Hello, World!"
NSAccessibilityPostNotificationWithUserInfo(NSApp.mainWindow!,
  NSAccessibilityNotificationName.announcementRequested,
  [NSAccessibilityNotificationUserInfoKey.announcement: message,
  NSAccessibilityNotificationUserInfoKey.priority:
  NSAccessibilityPriorityLevel.high.rawValue])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM