Keyboard shortcuts with UIKeyCommand in iPadOS 15 beta
Asked Answered
S

2

6

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.

Scrutinize answered 10/8, 2021 at 17:59 Comment(0)
S
7

Apparently, there is a new UIKeyCommand property wantsPriorityOverSystemBehavior, which needs to be set to true for some keys — like the ones I mentioned in my question: https://developer.apple.com/documentation/uikit/uikeycommand/3780513-wantspriorityoversystembehavior

Scrutinize answered 11/8, 2021 at 22:45 Comment(2)
Good answer ;) We are using this solution as well for arrow keys (on a MacCatalyst app) but it doesn’t seems enough to make the SPACEBAR key work though. Any clues? Thanks!Magnesia
I'm able to detect SPACEBAR key press using below code. UIKeyCommand(title: "Space"), action: #selector(handleKey(key:)), input: " ", modifierFlags: []) Hope this will help you.Dhumma
N
8

The solution is indeed to use wantsPriorityOverSystemBehavior. However, since you're using a subclass of UIResponder instead of adding the key commands for known keys, you may consider using the built-in method. It's more effective than adding each one separately and it's just more clean in terms of a pattern.

class ViewController: UIViewController {

    /// - SeeAlso: UIViewController.viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
        // do other things, nothing connected with UIKeyCommand
    }

    /// - SeeAlso: UIResponder.keyCommands
    override var keyCommands: [UIKeyCommand]? {
        let commands = [
            UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(actionUp)),
            UIKeyCommand(input: UIKeyCommand.inputDownArrow, modifierFlags: [], action: #selector(actionDown)),
            UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: [], action: #selector(actionLeft)),
            UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: [], action: #selector(actionRight))
        ]
        // if your target is iOS 15+ only, you can remove `if` and always apply this rule
        if #available(iOS 15, *) { 
            commands.forEach { $0.wantsPriorityOverSystemBehavior = true }
        }
        return commands
    }
}

private extension ViewController {
   
    @objc func actionUp() { print("up") }
    @objc func actionDown() { print("down") }
    @objc func actionLeft() { print("left") }
    @objc func actionRight() { print("right") }
}

I'm sorry it's an answer, but comments don't allow for a nice code syntax :-) Hope you don't mind my addition, but maybe someone doesn't know about keyCommands and find it useful in this context. At least I presented an example of how to use this new part of API which the creator @AlexStaravoitau already wrote about.


Apple claims, that:

Prior to iOS 15, the system delivered keyboard events to your key command objects first, and then to the text input or focus systems. If your app links against iOS 14 SDK or earlier, your app retains that behavior, even when running on iOS 15 or later.

and it's a pretty important little detail worth to keep in mind. I misread it and thought if you support iOS 14 or lower it'll work the old way. But it was my wrong understanding, and it's about linking at all (e.g. if you build with Xcode 12 or older and don't add extra iOS in the library). So, even though my app still supports iOS 12+, the key commands no longer works on new builds from Xcode 13 on iOS 15. It started to work only once I adapted the mentioned flag.

Nodus answered 3/12, 2021 at 18:57 Comment(1)
Thanks! Yep, I do override keyCommands property in my project, the example in my question is from a test project where I have been frantically trying to identify the issue. 😅 Very interesting re apps linked against iOS 14 SDK, thank you for sharing!Scrutinize
S
7

Apparently, there is a new UIKeyCommand property wantsPriorityOverSystemBehavior, which needs to be set to true for some keys — like the ones I mentioned in my question: https://developer.apple.com/documentation/uikit/uikeycommand/3780513-wantspriorityoversystembehavior

Scrutinize answered 11/8, 2021 at 22:45 Comment(2)
Good answer ;) We are using this solution as well for arrow keys (on a MacCatalyst app) but it doesn’t seems enough to make the SPACEBAR key work though. Any clues? Thanks!Magnesia
I'm able to detect SPACEBAR key press using below code. UIKeyCommand(title: "Space"), action: #selector(handleKey(key:)), input: " ", modifierFlags: []) Hope this will help you.Dhumma

© 2022 - 2024 — McMap. All rights reserved.