Skip to main content

无障碍

无障碍

¥Accessibility

Electron 应用中的可访问性问题与网站的可访问性问题类似,因为它们最终都是 HTML。

¥Accessibility concerns in Electron applications are similar to those of websites because they're both ultimately HTML.

手动启用辅助功能

¥Manually enabling accessibility features

Electron 应用将在辅助技术存在的情况下自动启用辅助功能(例如 Windows 上的 JAWS 或 macOS 上的 VoiceOver)。有关更多详细信息,请参阅 Chrome 的 无障碍文档

¥Electron applications will automatically enable accessibility features in the presence of assistive technology (e.g. JAWS on Windows or VoiceOver on macOS). See Chrome's accessibility documentation for more details.

你还可以在 Electron 应用中或通过在第三方原生软件中设置标志来手动切换这些功能。

¥You can also manually toggle these features either within your Electron application or by setting flags in third-party native software.

使用 Electron 的 API

¥Using Electron's API

通过使用 app.setAccessibilitySupportEnabled(enabled) API,你可以在应用首选项中手动向用户公开 Chrome 的辅助功能树。请注意,用户的系统辅助实用程序优先于此设置,并将覆盖它。

¥By using the app.setAccessibilitySupportEnabled(enabled) API, you can manually expose Chrome's accessibility tree to users in the application preferences. Note that the user's system assistive utilities have priority over this setting and will override it.

在第三方软件内

¥Within third-party software

苹果系统

¥macOS

在 macOS 上,第三方辅助技术可以通过以编程方式设置 AXManualAccessibility 属性来切换 Electron 应用内的辅助功能:

¥On macOS, third-party assistive technology can toggle accessibility features inside Electron applications by setting the AXManualAccessibility attribute programmatically:

使用 Objective-C:

¥Using Objective-C:

CFStringRef kAXManualAccessibility = CFSTR("AXManualAccessibility");

+ (void)enableAccessibility:(BOOL)enable inElectronApplication:(NSRunningApplication *)app
{
AXUIElementRef appRef = AXUIElementCreateApplication(app.processIdentifier);
if (appRef == nil)
return;

CFBooleanRef value = enable ? kCFBooleanTrue : kCFBooleanFalse;
AXUIElementSetAttributeValue(appRef, kAXManualAccessibility, value);
CFRelease(appRef);
}

使用 Swift:

¥Using Swift:

import Cocoa
let name = CommandLine.arguments.count >= 2 ? CommandLine.arguments[1] : "Electron"
let pid = NSWorkspace.shared.runningApplications.first(where: {$0.localizedName == name})!.processIdentifier
let axApp = AXUIElementCreateApplication(pid)
let result = AXUIElementSetAttributeValue(axApp, "AXManualAccessibility" as CFString, true as CFTypeRef)
print("Setting 'AXManualAccessibility' \(error.rawValue == 0 ? "succeeded" : "failed")")