I am working on a MacOS app, and I cannot figure out how to listen to accessibility status changes (for example, when VoiceOver is turned on or off).
In iOS, there is a notification I can listen to, UIAccessibilityVoiceOverStatusDidChangeNotification
.
Is there an equivalent in MacOS?
It turns out there is a hidden api to listen to the accessibility notification. You can listen to @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification"
[center addObserver:self
selector:@selector(onAccessibilityStatusChanged:)
name:@"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification"
object:nil];
and then in the method, you can check voiceOverEnabled which is introduced in 10.13
if(@available(macOS 10.13, *)) {
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSLog(@"got notification voiceover enabled ? %d",ws.voiceOverEnabled);
}
On macOS, the correct way to do this is via KVO:
[[NSWorkspace sharedWorkspace] addObserver:self forKeyPath:@"voiceOverEnabled" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
with the accompanying handler method:
- (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];
}
}
On iOS and tvOS, use:
[[NSNotificationCenter defaultCenter] addObserverForName:UIAccessibilityVoiceOverStatusDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
// handle the event in your way.
}];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.