简体   繁体   English

如何检测用户是否启用了 Mac OS 高对比度辅助功能设置?

[英]How do I detect if a user has Mac OS high contrast accessibility settings enabled?

I have a React/TypeScript project, and I am trying to detect if a user has any of Mac OS's high contrast accessibility settings enabled: Invert colors, Use grayscale, Differentiate without color, Increase contrast, or an increased Display contrast setting.我有一个 React/TypeScript 项目,我正在尝试检测用户是否启用了 Mac OS 的任何高对比度可访问性设置:反转 colors、使用灰度、无颜色区分、增加对比度或增加显示对比度设置。

I want to detect these using JavaScript/TypeScript.我想使用 JavaScript/TypeScript 检测这些。

So far, I can detect only Invert colors .到目前为止,我只能检测到Invert colors


How do I detect if a user has any of the other Mac OS accessibility settings enabled?如何检测用户是否启用了任何其他 Mac OS 辅助功能设置?


More information:更多信息:

Iam afraid it is not possible its at this moment.恐怕目前不可能。

The feature in Objective-C to determine if the accessibility mode is active (boolean AXAPIEnabled(void);) has been deprecated as of 10.9 with no indication that there is a replacement.自 10.9 起,Objective-C 中用于确定可访问性模式是否处于活动状态(布尔 AXAPIEnabled(void);)的功能已被弃用,没有迹象表明有替代品。 You can see it on the Apple Developer site.你可以在 Apple Developer 网站上看到它。 By extension, if developers at that level of the system no longer have access, I expect web developers would also be restricted (just as AppleScript writers seem to not have access).通过扩展,如果该系统级别的开发人员不再具有访问权限,我预计 web 开发人员也会受到限制(就像 AppleScript 编写人员似乎没有访问权限一样)。

In general, testing for the presence of assistive technology or activation of assistive features is frowned upon by the users who need it most.通常,最需要的用户不赞成测试是否存在辅助技术或激活辅助功能。 It creates the very real risk of both well-intentioned developers breaking these features (perhaps by un-verting the page) and also of allowing bad actors to determine somebody's personal health information.它给善意的开发人员破坏这些功能(可能是通过取消页面翻转)以及允许不良行为者确定某人的个人健康信息带来了非常现实的风险。

Altought it might come in future as its in stage 5 (proposal) https://drafts.csswg.org/mediaqueries-5/#prefers-contrast尽管它可能会在未来作为第 5 阶段(提案) https://drafts.csswg.org/mediaqueries-5/#prefers-contrast

Other than that Iam afraid you are facing dead end.除此之外,我恐怕你正面临死胡同。

On my last project, this was handled via a little bit UX knowledge.在我的上一个项目中,这是通过一点 UX 知识来处理的。 We went down same rabbit hole but what ended up working was user's ability to toggle some accessibility settings manually including High Contrast.我们进入了同一个兔子洞,但最终起作用的是用户能够手动切换一些可访问性设置,包括高对比度。 I dont think you should invest too much time in answering "how to detect" as in some edge cases your effort will be bypassed if users interact via devices they dont have too much control over (shared PC, outdated devices, OS bugs etc.) Heck, some new monitors provide this feature builtin which is even harder to detect.我认为您不应该在回答“如何检测”问题上投入太多时间,因为在某些边缘情况下,如果用户通过他们没有太多控制权的设备(共享 PC、过时设备、操作系统错误等)进行交互,您的努力将被绕过。哎呀,一些新的显示器提供了这个更难检测到的内置功能。

But, nothing as reliable as good ol' "Hello {user.name}, here are some settings you can toggle manually in the upper-right".但是,没有什么比“您好 {user.name},您可以在右上角手动切换的一些设置”更可靠的了。

Happy coding!快乐编码!

For Electron you can use the nativeTheme API and access the nativeTheme.shouldUseHighContrastColors property.对于 Electron,您可以使用nativeTheme API并访问nativeTheme.shouldUseHighContrastColors属性。

A Boolean for if the OS / Chromium currently has high-contrast mode enabled or is being instructed to show a high-constrast UI. Boolean 如果 OS / Chromium 当前启用了高对比度模式或被指示显示高对比度 UI。

// in main process
const {nativeTheme} = require('electron');

console.log(nativeTheme.shouldUseHighContrastColors);

Note to OP : I understand this does not solve your issue as you are trying to do this in a browser not an electron app, however I figure that this question may come up in search results for people who are looking to add this functionality in an electron app so I figured I would add an answer. OP 注意:我知道这不能解决您的问题,因为您尝试在浏览器中执行此操作而不是electron 应用程序,但是我认为对于希望在electron 应用程序所以我想我会添加一个答案。

This is an old question (2019), but I was wondering this same thing myself in 2022. At least the current macOS Monterey has an accessibility feature "Increase contrast" which state is possible to be detected by:这是一个老问题(2019 年),但我自己在 2022 年也想知道同样的事情。至少当前的 macOS Monterey 具有可访问性功能“增加对比度”,state 可以通过以下方式检测到:

const prefersContrast = matchMedia("(prefers-contrast: more)");

The other states are "no-preference", "less" and "custom".其他状态是“无偏好”、“较少”和“自定义”。 See here: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-contrast见这里: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-contrast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 辅助功能 - 如何找到对比度问题? - Accessibility - how do I find contrast problems? 如何检测窗口是否处于高对比度模式,或者显示大字体还是超大字体? - how do I detect whether windows is in high contrast mode, or it displays large font or extra large font? 如何检测不同的用户设置? 启用Javascript,启用Cookie - How to detect different user settings? Javascript enabled, Cookies enabled IE文本大小(例如“ EG”为“最大”)或任何其他辅助功能设置(高对比度等)的Javascript测试 - Javascript test for IE Text Size (EG “Largest”) or any other accessibility settings (high contrast etc) 如何检测用户是否在浏览器中启用了全屏 - How to detect if user has enabled full screen in browser 如何检测用户浏览过的站点? - How do I detect the site a user has navigated from? 如何通过JavaScript或CSS检查用户是否处于高对比度模式 - How to check if user is in high contrast mode via JavaScript or CSS 检测用户是否在域上启用了位置服务 - Detect if user has enabled location services on domain 在 Chrome 浏览器中检测高对比度扩展程序的使用 - Detect High Contrast extension use in Chrome browser 如何检测客户端浏览器中是否启用了 ActiveX? - How do I detect if ActiveX is enabled in the browser of client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM