pkamb
pkamb

Reputation: 34983

Force NSAccessibility Voiceover to read unfocused OS X textfield?

I have a button, that when pressed, shows a "helper" window.

This window is shown, but not given keyboard focus, via orderFrontRegardless.

The window contains an NSTextView with helper text inside.

For vision-impaired users, I would like OS X's voiceover to immediately read the contents of this window's text view when it appears.

I am attempting to make VoiceOver read the text via:

NSAccessibilityPostNotification(textView, NSAccessibilityValueChangedNotification);

In my subclass of NSTextView I then override the accessibility method:

- (id)accessibilityAttributeValue:(NSString *)attribute
{
    //The notification calls this method for attributes:
    //AXRole: returns AXTextArea
    //AXSharedCharacterRange: returns range of the text view

    return [super accessibilityAttributeValue:attribute];
}

The notification causes it to query for AXRole (NSAccessibilityRoleAttribute) and AXSharedCharacterRange (NSAccessibilitySharedCharacterRangeAttribute).

The character range correctly returns the range of the text area.

However, at no point is AXValue (NSAccessibilityValueAttribute) requested. That is that I am expecting is required when wanting VoiceOver to read the textfield.

Why is the NSAccessibilityValueChangedNotification not requesting the TextView's NSAccessibilityValueAttribute? How do I make VoiceOver read the text area's text?

Upvotes: 5

Views: 2718

Answers (3)

Vadim Peretokin
Vadim Peretokin

Reputation: 2816

You can now do it using NSAccessibilityAnnouncementRequestedNotification.

See https://developer.apple.com/documentation/appkit/nsaccessibilityannouncementrequestednotification?language=objc.

Upvotes: 1

lead_the_zeppelin
lead_the_zeppelin

Reputation: 2052

Override

-(NSArray *)accessibilityAttributeNames
{
    NSMutableArray *attributeNames = [[super accessibilityAttributeNames] mutableCopy];
    [attributeNames addObject:NSAccessibilityValueAttribute];
    return attributeNames;
}

Have you tried NSLog into there to find out if NSAccessibilityValueAttribute is an attribute at all?

Upvotes: 0

ppaulojr
ppaulojr

Reputation: 3647

I believe there's no way you can do it without setting the focus to the desired NSTextView.

I have found a solution using iOS that maybe can be implemented in this OSX application: VoiceOver: force an accessibility element to be selected after a screen transition

In iOS you can pass in an accessibility element when posting a UIAccessibilityLayoutChangedNotification or UIAccessibilityScreenChangedNotification as the second argument to UIAccessibilityPostNotification and VoiceOver will focus on that element.

Maybe you can use a similar approach with NSAccessibility framework with NSAccessibilityPostNotification

Upvotes: 1

Related Questions