Chapters

Hide chapters

Catalyst by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

6. The Keyboard
Written by Andy Pereira

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The physical keyboard is something that has always been part of the personal computer experience. But, when iOS was introduced, it almost seemed like the idea of a software keyboard would take over. Luckily, for those that like a real keyboard, Apple introduced support not only for physical keyboards on iOS but keyboard shortcuts.

While keyboards shortcuts might go overlooked on iOS apps, macOS users expect them. In this chapter, you’ll learn how to add shortcuts, what modifier keys are, and how to combine them with key combinations to quickly perform tasks in your app.

Getting started

Open the starter project for the chapter. It will be best if you can run this app on a physical iOS or iPadOS device, with a physical keyboard paired with the device. Using the simulator will work, but you may notice some performance issues.

First responders

Before you add the shortcuts, it will help to understand a little bit about the responder chain and the first responder. UIViewControllers, UIViews and UIApplication are all classes that can receive and handle events, otherwise known as responder objects. Since you can add keyboard shortcuts to any of these kinds of classes, you’ll need to tell the system which responder is the class in the responder chain that will receive the keyboard event first. This is referred to as the first responder.

// MARK: - Keyboard Commands
override var canBecomeFirstResponder: Bool {
  return true
}

Adding the commands

Keyboard shortcuts can be performed by simply pressing a single key on the keyboard or multiple keys. They can also be used in conjunction with modifiers keys, like Command, Control and Option. You’re going to add three shortcuts that each use modifier keys.

@objc private func addEntry(sender: UIKeyCommand) {
}

@objc private func goToPrevious(sender: UIKeyCommand) {
}

@objc private func goToNext(sender: UIKeyCommand) {
}
override var keyCommands: [UIKeyCommand]? {
  let newKeyCommand
    = UIKeyCommand(input: "N",
                   modifierFlags: .control,
                   action: #selector(addEntry(sender:)))
  newKeyCommand.discoverabilityTitle = "Add Entry"
  return [newKeyCommand]
}

DataService.shared.addEntry(Entry())
override var keyCommands: [UIKeyCommand]? {
  let newKeyCommand
    = UIKeyCommand(input: "N",
                   modifierFlags: .control,
                   action: #selector(addEntry(sender:)))
  newKeyCommand.discoverabilityTitle = "Add Entry"
  let upKeyCommand
    = UIKeyCommand(input: "[",
                   modifierFlags: [.command, .shift],
                   action: #selector(goToPrevious(sender:)))
  upKeyCommand.discoverabilityTitle = "Previous Entry"
  let downKeyCommand
    = UIKeyCommand(input: "]",
                   modifierFlags: [.command, .shift],
                   action: #selector(goToNext(sender:)))
  downKeyCommand.discoverabilityTitle = "Next Entry"
  return [newKeyCommand, upKeyCommand, downKeyCommand]
}

guard let navigationController =
  viewControllers.first as? UINavigationController,
  let mainTableViewController =
  navigationController.topViewController
    as? MainTableViewController else { return }
mainTableViewController.goToPrevious()
guard let navigationController =
  viewControllers.first as? UINavigationController,
  let mainTableViewController =
  navigationController.topViewController
    as? MainTableViewController else { return }
mainTableViewController.goToNext()
func goToPrevious() {
  guard let index = indexOfCurrentEntry(),
    index > 0 else { return }
  let previousIndex = index - 1
  let indexPath = IndexPath(row: previousIndex,
                            section: 0)
  tableView.selectRow(at: indexPath,
                      animated: false,
                      scrollPosition: .middle)
  performSegue(withIdentifier: "ShowEntrySegue",
               sender: tableView.cellForRow(at: indexPath))
}

func goToNext() {
  guard let index = indexOfCurrentEntry(),
    index < DataService.shared.allEntries.count - 1 else { return }
  let nextIndex = index + 1
  let indexPath = IndexPath(row: nextIndex,
                            section: 0)
  tableView.selectRow(at: indexPath,
                      animated: false,
                      scrollPosition: .middle)
  performSegue(withIdentifier: "ShowEntrySegue",
               sender: tableView.cellForRow(at: indexPath))
}
let deleteKeyCommand
  = UIKeyCommand(input: "\u{8}",
                 modifierFlags: [],
                 action: #selector(removeEntry(sender:)))
deleteKeyCommand.discoverabilityTitle = "Delete Entry"

return [newKeyCommand, upKeyCommand, 
  downKeyCommand, deleteKeyCommand]
@objc private func removeEntry(sender: UIKeyCommand) {
  guard let navigationController = viewControllers.first
    as? UINavigationController,
    let mainTableViewController 
      = navigationController.topViewController
      as? MainTableViewController else { return }
  mainTableViewController.deleteCurentEntry()
}
func deleteCurentEntry() {
  guard let index = indexOfCurrentEntry() else { return }
  DataService.shared.removeEntry(atIndex: index)
}

Where to go from here?

Your app is now set to handle shortcuts. While Catalyst will automatically handle these keyboard shortcuts, you’ll learn later on how to make sure these shortcuts are shown in the Menu bar.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now