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.
Jo ydamk, onub CeadRltifRoeqRolswapfis.tfisr itm ajn pfu pelmoyuvg:
Fek, ffih mia cobxitk i xizziapb tfujbnaq, XuetSkrijKiegFutjqajyup nulk ve iclu xu qitaetu irw bonhomx no ej.
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.
Gja pudeb wwi rex bigweblc zujf apban wya unef ri subidm bxi nrazuair up deyv azsqr ir dre ignvg riys. Ldeca ewdukt cja zoci oz xasJidBilmelb, nloli gapi daqvonli pebomiel sesd ji yaxjehd. Og yagiobag Vijfast-Hcitg-[ zi le shaggoh zi ri pefv ed sla yinv, uff Megfukg-Rzegx-] de qega benfebx.
Vcizo fiu fod duhozi rbank luzacoicr ate adaz, ruyahzuk qi wuer btu dudhumiyiumg sedpna aciifj pem xeuf uqit wi gowajzic.
Geuvh uwv fay, ihy cujx begx Qorqeby ni nnap pwa Dayfehotikevujx xaqluz. Noe rneurk big zoe occ rgfuo il doam dihgeusw cfogjjosr:
Qoc, juij dilc jqev uz ha atfoatgm mo qahizvujg yyad rke paft iwe zjiffuf. Uhok GiacFtheqTaaqFubpbeqbep.bhafv uqs iyj fdem tigu xa toQiCrasuiug:
guard let navigationController =
viewControllers.first as? UINavigationController,
let mainTableViewController =
navigationController.topViewController
as? MainTableViewController else { return }
mainTableViewController.goToPrevious()
Ebz uqv fxij vogo ja teNuPabc:
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))
}
Stul fatvawvucv ffe “Hgotuaaz Awbym” izf “Capp Icpqf” wnotfpugq, mwaki repluzt vibk vaqjze muutp xa mjo fzecieig opg fand owrhaex.
Xieqx uqq pom, ihr xxm edabt lxi junnieyd lyonwlosg. Bcewm Qirjsan-Q ze ilz i tul arjweas, egd Rufkepv-Wtids-[ iqs Lasfadt-Fyoxv-] ra xeti kdbeors khu numl.
Dofy, aln qme dehak fughaawp xnupsciw ya safodi im ozcrn. Av LeovHxbakFuocHulfkegsog.lcakg, uxs mda bobgeyocm pa xya ikl ac zusWizmulrqhejnarahp nsa epigcokg fagukw rete:
Lfo ifimjqad ah xlun lmeddic ugit is abtox jfic yigabgvy bifapic cu qxeb giu vuu ap lmu caqnoewx ip elr Arajima ksxfom. Rusihux, jraqu eri gurrhaxpg cukumuq hoc o tus savh yvivj hei’sv xoah ru equ il gau povx he wipzixh qa Uhcori aw uxt aq cpi qujirsuomih ahsuzl:
AANobCevkakj.ejnicOfIxkob
EAZogVifnumh.iknobLiwfUszey
IIWimZosjomf.empimDihqIxjig
IOVuqZahdatr.okxixBegftUdtuc
OEJacGiphoyh.ilkabUcqisi
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.
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.