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

谁都会走 提交于 2020-06-25 03:53:12

问题


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.

I want to detect these using JavaScript/TypeScript.

So far, I can detect only Invert colors.


How do I detect if a user has any of the other Mac OS accessibility settings enabled?


More information:

  • The prefers contrast media query is not yet available. I tried it anyway, and it doesn't work.
  • The inverted colors media query tests only for the Invert colors setting.
  • Apple's dev docs on color and contrast do not have a solution.
  • Detecting high contrast use in other browser, OS scenarios

回答1:


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. You can see it on the Apple Developer site. 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).

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

Other than that Iam afraid you are facing dead end.




回答2:


On my last project, this was handled via a little bit UX knowledge. 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.

But, nothing as reliable as good ol' "Hello {user.name}, here are some settings you can toggle manually in the upper-right".

Happy coding!




回答3:


For Electron you can use the nativeTheme API and access the nativeTheme.shouldUseHighContrastColors property.

A Boolean for if the OS / Chromium currently has high-contrast mode enabled or is being instructed to show a high-constrast 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.



来源:https://stackoverflow.com/questions/58964264/how-do-i-detect-if-a-user-has-mac-os-high-contrast-accessibility-settings-enable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!