我正在开发一个MacOS应用程序,我不知道如何收听辅助功能状态的更改(例如,当VoiceOver打开或关闭时(。
在iOS中,有一个通知我可以收听,UIAccessibilityVoiceOverStatusDidChangeNotification
。
MacOS中有类似的软件吗?
原来有一个隐藏的api来监听可访问性通知。您可以收听@"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification"
[center addObserver:self
selector:@selector(onAccessibilityStatusChanged:)
name:@"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification"
object:nil];
然后在该方法中,您可以检查10.13 中引入的voiceOverEnabled
if(@available(macOS 10.13, *)) {
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSLog(@"got notification voiceover enabled ? %d",ws.voiceOverEnabled);
}
在macOS上,正确的方法是通过KVO:
[[NSWorkspace sharedWorkspace] addObserver:self forKeyPath:@"voiceOverEnabled" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
附带的处理程序方法:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"voiceOverEnabled"]) {
// handle the event in your way.
//
} else {
// Any unrecognized context must belong to super
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
在iOS和tvOS上,使用:
[[NSNotificationCenter defaultCenter] addObserverForName:UIAccessibilityVoiceOverStatusDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
// handle the event in your way.
}];