Keyboard shortcuts on iOS
Most people operate their smartphones with touch, but some people use an external keyboard. Learning and using keyboard shortcuts can save you a lot of time. Shortcuts should always use a modifier key, such as CTRL
or CMD
to avoid clashing with system shortcuts.
On iOS, the pressesBegan
and pressesEnded
can be used to activate shortcuts. But, you should use UIKeyCommand
to add keyboard shortcuts. By adding modifierFlags
you can be sure that shortcuts are not activated by accident. An additional advantage is that UIKeyCommand
-shortcuts are shown when long pressing the command key.
let find = UIKeyCommand(
input: "f",
modifierFlags: .command,
action: #selector(findContent),
discoverabilityTitle: "Find"
)
override var keyCommands: [UIKeyCommand]? {
return [find]
}
@objc private func find() {
// Logic
}