如何判断系统偏好设置中的 Voice Over 是否已打开?

发布于 2024-07-12 09:15:42 字数 64 浏览 9 评论 0原文

有没有一种方法(最好向后兼容 Mac OS X 10.3)来判断系统偏好设置中是否激活了“Voice Over”?

Is there an way, ideally backwards compatible to Mac OS X 10.3, to tell if "Voice Over" is activated in System Preferences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角迷惘 2024-07-19 09:15:43

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

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])
给不了的爱 2024-07-19 09:15:42

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

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

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"));
潦草背影 2024-07-19 09:15:42

如果有人有同样的问题,很高兴知道现在可以通过方便的界面访问 Voice Over 状态:

NSWorkspace.shared.isVoiceOverEnabled

If anyone has the same question, it could be good to know, that Voice Over status is accessible via convenient interface now:

NSWorkspace.shared.isVoiceOverEnabled
风吹雪碎 2024-07-19 09:15:42

基于 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 和辅助功能是非常重要的主题,但令人遗憾的是,苹果文档的缺乏,尤其是针对 macOS 的文档,使得开发人员很难正确实现它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文