Keyboard shortcuts that use the Command key modifier are not handled properly, and the UIKeyCommand action and pressesBegan and pressesEnded methods are not called at all or are called unreliably.
It is easy to reproduce using this snippet:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textView = MyTextView()
textView.font = UIFont.systemFont(ofSize: 24)
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec efficitur eros vitae dui consectetur molestie. Integer sed massa rutrum, pharetra orci eget, molestie sem. Fusce vestibulum massa nisi, vitae viverra purus condimentum et. Sed nec turpis aliquam, tempus enim sit amet, gravida libero. Praesent scelerisque venenatis nunc, vel convallis nisl auctor vitae. Mauris malesuada tempus pharetra. Nullam ornare venenatis ullamcorper. In viverra feugiat tincidunt. Nullam iaculis urna eu semper rutrum. "
textView.isEditable = true
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
}
class MyTextView: UITextView {
override var keyCommands: [UIKeyCommand]? {
[
UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(commandAction(_:)))
]
}
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
print("pressesBegan")
super.pressesBegan(presses, with: event)
}
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
print("pressesEnded")
super.pressesEnded(presses, with: event)
}
@objc private func commandAction(_ sender: Any?) {
print("commandAction")
}
}
Run the code in a Simulator or on a Device with an external keyboard connected. Observe the console for a string "commandAction" when pressing the combination Command + [ on the keyboard. Result it not predictable, the UIKeyCommand is not called at all, or called in a loop, or sometimes called after change selection in the UITextView. The same with pressesBegan and pressesEnded. Compare results with the change where instead of Command modifier, we use Control modifier eg.: "UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(commandAction(_:))" - now each keyboard shortcut is properly reported to methods.
The UIKeyCommand.wantsPriorityOverSystemBehavior property changes nothing.
Behavior reproducible in the Simulator and on the Device (iPad)
(the issue was confirmed during online WWDC24 Labs)
Reported as FB13897415