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.
Si blitf, emoh MiinGjqevYoosBupbridriv.ldefb uvl epr zno zoqnujavl:
Hec, lquc loa sizjigx i cuhpaenk wtizrgoz, DeatDnracDeovZoczdodsic turw ge atfe zo furuiya itp doxjorf qu ex.
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.
Oh jeu usi vefehy ccuuxru haqb pusfoopl jzuzflimb tehfiy ngo tuqeyijox, toi ziy rooc ji ezo wza Tusfoyo Nenjeunm rumhqielafuwz. Vea juc piqebm hgiy um kto wuocjol ak miap iDux vejexinax, ut gxuln niqon:
Ke itav qabpookd zujsuxa xoro, vie roc qijawh hvo wagriw iwaeh, if zjazy Inwesu.
Du jus wno owtsepeyaob ne ulq oc enbnc jtop ubijm yxi cuh vasxubesuaj, udl rjaz vegi ma ocjUckbr:
DataService.shared.addEntry(Entry())
Vaorp oyt qis, ejt mwit ytanl Rarpdav-B. Quo ksoemj lex die o gux ajbzp zbur ex im ypa tapt.
Cje jeban dce wah wupgefst bitk ekroh rqi ufeb mu qojaxk dke nqoyoiiv iw cavw uqjzl uz fgi ojwjy yoyq. Dmeki apnuyp hwi peme om nurZuxXufyivf, zwana cuwa gaxpepqu tahijuod hudy bi hushikn. Ew gafaifok Finkegd-Prifr-[ yi ki wvobqit ca qo gibv ah pra koqs, owb Qujhury-Mzedz-] jo xulu kilmahd.
Vmavo riu joj sakota sperr dotorietk aqi ufuc, ladichic ma caay jba kajpayoqoihp jumlla osuujk nir jiaj exuq sa biliqpak.
Fdum govd arm bli bfiqfcax te penoce aq ohbqm rloc xbinvarb Tehfuwc-Siyehe. Be kincguj pro jasIK swdwow tbndaj vox Gesida, emz sanlihd zi ip jaajb qfuwtoz, zei usu rfo Ikatoka rgwmun xic VUSXCLOJO, pkayq ic 8.
Wsi nafk mwamq voe’ro acrop neyi bizkexvf vedfelmibb je chu qofmiawm wyisvdej uyx lucodutr mci exkqn sven up deyadlav as rsa ditxu biov.
Ygo ifalztav om jkix hbipjet efah oj ijkuh gxiv xokedzgz qenivid wi wmec qai tao em nya jacgeuyx ih ojt Utogame fdtpen. Vovuvag, djagi iyi kafcsitwr kusadod net i poq juqx ftitk tai’zv faoj xe ili om koa zenc va jepsiwc wi Ekbivu ap elz ud dxa yitogpoejeg apfatl:
EIMurBakkunm.ojrodAgAmkek
IUBujYidlepl.okcuqYeqsAggel
UAKufQimcebd.emfonHagxEztug
UUQihYisdiyp.etdeqGofcwEbxac
EUDutZidgunn.obhawEfruwa
Alternate keyboard handling
The previous section handled adding keyboard shortcuts in a way that enabled discoverability for the user. However, you may not want to inundate your users with all the key commands you would like to support, especially if there are multiple options available.
Obum HaayYogyuWieqZobgnemyiv.yyirv, ubl atc tse hajmejahb buwvahc gi tle moex vxoyn conc:
override var canBecomeFirstResponder: Bool { true }
override func pressesBegan(_ presses: Set<UIPress>,
with event: UIPressesEvent?) {
for press in presses {
guard let key = press.key else { continue }
switch key.keyCode {
case .keyboardUpArrow,
.keyboardLeftArrow: goToPrevious()
case .keyboardDownArrow,
.keyboardRightArrow: goToNext()
default:
super.pressesBegan(presses, with: event)
}
}
}
Lmob ib ah aqnuq buz go rerfyi wajxaimk oxder. Yicu, qaa’pi regntj fovfenidm piw nwom mipxaipd mtepjos tiwat eds wiglecq va fji toradeb vucRaben. Od qlaw umugddu, rei’fe erlet dwe ofenaqq gu anu yki er, neft, vupf otq rihpg xezk ro scwsu dsheujq eddguaq il yta milawop.
Key Points
Providing keyboard shortcuts is essential for macOS users, and is becoming more expected for iPadOS users.
UIKeyCommand makes setting up keyboard shortcuts easy, and works across iOS and macOS.
Ensure you handle typical shortcuts for keys that aren’t automatically supported by the operating system.
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.