Keyboard shortcuts with UIKeyCommand in iPadOS 15 beta

For some reason I can't get hardware keyboard shortcuts to work in iPadOS 15 (beta 5). They work for most keys, but not for arrow keys and tab key.

The same code seems to work well when compiled in Xcode 13 (beta 4) and run on iPadOS 14.5 simulator, but then refuses to work when built with same Xcode but on iPadOS 15 sim. I've tried it on actual devices with iPadOS 15 betas up to 5 with same results.

Here is a minimal example:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        addKeyCommand(UIKeyCommand(title: "UP", action: #selector(handle(key:)), input: UIKeyCommand.inputUpArrow, modifierFlags: []))
        addKeyCommand(UIKeyCommand(title: "DOWN", action: #selector(handle(key:)), input: UIKeyCommand.inputDownArrow, modifierFlags: []))
        addKeyCommand(UIKeyCommand(title: "TAB", action: #selector(handle(key:)), input: "\t", modifierFlags: []))
    }

    @objc func handle(key: UIKeyCommand?) {
        NSLog("Intercepted key: \(key?.title ?? "Unknown")")
    }
}

I haven't found any related reports or open radars, so I'm suspecting I could be missing something here. If this should be reported, where do I report a bug like that?

Thank you.

Answered by sjs in 684199022

For those keys, you probably need to set wantsPriorityOverSystemBehavior to YES.

Accepted Answer

For those keys, you probably need to set wantsPriorityOverSystemBehavior to YES.

Here is the sample code from the viewDidLoad method.

        if #available(iOS 13.0, *) {
            let upKeyCommand = UIKeyCommand(title: NSLocalizedString("Up", comment: "Up"), action: #selector(handleExternalKeyPress(key:)), input: UIKeyCommand.inputUpArrow, modifierFlags: [])
            if #available(iOS 15.0, *) {
                upKeyCommand.wantsPriorityOverSystemBehavior = true
            } else {
                // Fallback on earlier versions
            }
            addKeyCommand(upKeyCommand)
        } else {
            // Fallback on earlier versions
        }

Thank you @sjs for your inputs.

Keyboard shortcuts with UIKeyCommand in iPadOS 15 beta
 
 
Q