简体   繁体   中英

How to Implement accessibilityCustomActions for VoiceOver on Mac?

I have a button that responds to various mouse clicks (regular click, right click, control+click, option+click, command+click...) to show different popup menus. Since it would be annoying for VoiceOver users to use actual physical mouse, I would like to map those to different VoiceOver actions.

However, I'm not getting the results I expected. Could someone help me to understand better what I'm missing? Here is what I discovered so far.

If I subclass NSButton and override the following functions, they work fine. Except there's one odd thing. If I press vo+command+space to bring up the list of available actions, VoiceOver says Action 1 instead of Show Menu.

override func accessibilityPerformPress() -> Bool { 
    print("Pressed!") 
    return true 
}

override func accessibilityPerformShowAlternateUI() -> Bool { 
    print("Show Alternate UI") 
    return true 
} 

override func accessibilityPerformShowMenu() -> Bool { 
    print("Show Menu") 
    return true 
} 

In the same NSButton subclass, if I also override accessibilityCustomActions function, "Do Something" never comes up in the list of available actions when I press vo+command+space.

override func accessibilityCustomActions() -> [NSAccessibilityCustomAction]? { 
    let custom = NSAccessibilityCustomAction(name: "Do Something", target: self, selector: #selector(doSomething)) 
    return [custom] 
} 

@objc func doSomething() -> Bool { 
    print("Done something.") 
    return true 
} 

If I subclass NSView instead of NSButton , and override the same functions from #1, everything works fine. Unlike first case, even VoiceOver correctly says "Show Menu" for the action from accessibilityPerformShowMenu instead of "Action 1".

in the same NSView subclass, if I override accessibilityCustomActions along with accessibilityPerformPress , accessibilityPerformShowMenu , or accessibilityPerformShowAlternateUI , "Do Something" doesn't come up in the action list.

However, "Do Something" does come up in the action list if I just override accessibilityCustomActions by itself without accessibilityPerformPress , accessibilityPerformShowMenu , and accessibilityPerformShowAlternateUI .

I tried creating another action with the name "Press" that does the same thing when pressing vo+space, and including in the return value of accessibilityCustomActions . However, Vo+space did not trigger the action. Instead, I had to press vo+command+space, and then select "Press". I guess the action just has the name "Press", but it's not actually connected to vo+space. I'm not sure how I can actually make that particular custom action to respond to vo+space.

I would appreciate if someone could help me to implement accessibilityCustomActions as well as accessibilityPerformPress , accessibilityPerformShowMenu , and accessibilityPerformShowAlternateUI together into NSButton .

Thanks so much!

The problem is that you are overriding these AX methods on the NSButton , not the NSButtonCell . For nearly everything to do with accessibility in NSControl s, you will want to deal with the NSCell in question. If you use the custom action code you've written above and stick it in a subclass of NSButtonCell used by your button, then it will work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM