I am working with VoiceOver and an external keyboard. I need to detect when an user activates an element using keyboard or tap.

I am working with VoiceOver and an external keyboard. I need to detect when an user activates an element using keyboard or tap.

To achieve this, I have implemented:

public override func pressesEnded(_ presses: Set<UIPress>, 
                                 with event: UIPressesEvent?) {
        guard let key = presses.first?.key else { return }
        switch key.keyCode {
        case .keyboardSpacebar:
            addSelectedTraits()
        default:
            super.pressesEnded(presses, with: event)
        }
}

The problem is that the space bar is not triggering this function as any other keys do. What could be the problem? Notice that I have activated the full access keyboard on my iPhone, this makes the space bar able to perform activation actions.

Can you tell me more about your use case? You mentioned wanting to detect when a user activates an element, you can override the method accessibilityActivate() on any accessibility element to run custom code, so perhaps you can run whatever method you're trying to call instead of checking for this via keyboard events. Try this out and see if it works for your use case

https://developer.apple.com/documentation/objectivec/nsobject/1615165-accessibilityactivate

I am working with VoiceOver and an external keyboard. I need to detect when an user activates an element using keyboard or tap.
 
 
Q