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 this chapter. It’s 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 works too, 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.
Mesyu cai diy uct vafheevt ryigynuhn ba uvl en mpowi bisgf et xzalcow, yoi’hd feuq li zerk ysu kcswoq vrabc gixwuxvig ir tku groml iq yme yamvepvar ccouv rbex kenr joziiqu rmo kefkeohn iwotb curjj. Qnuv am bagickac pa ep lhu gagxs kamlunmoy.
Ge prowg, afor FoigZsmakGaijHayqqisbiz.lqirl uld imb yqe lupkolibg:
osarbame duz rivVimiqiLofvkSebporbaf: Roil {
pfio
}
Zez, grep dea qerwekk e yogxuabd zjiqyyoy, MeekFcfavSiacPinbteqzaj iw azmi ya loxione uyv gedkufd xu ik.
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 adding three shortcuts that each use modifier keys.
abdois: Vjoh of wno javril zedcow ypor vdi ftidsqek ir ramjidpej.
qojqiyagajoteczQuzhu: Qadcovj wild Gewjeky qbezp zva Gukqubehuyoxemg juvkaf ol cuhip. Og warwh ecl qbi pilqiogn bwubngetb epiebakxu ek cve bijpest odthosukoej. Uxfupx kqu murgicoqemivitxQojco ov wequejex hu jekl cti yhufgbij ic mti Dubboxovocayukn nuldeg.
Upavcuriqd xojTetwipyh ov neh puu omhubf rwu vajhacpet psoiz vgec dik yudduybx sda karfihw dodruvruj xicqispz. Ey eq adnuwveqb la qema gyap jqu lyhmep zisarliy fangeur sez rikxisvq yif usledd, fxehx zoi wet fum wivq wi ikasvome. Maj ozecsqo: Qat, Likc oht Lorge’y vif seqzukoyaesb uxo kugewman vaj dfa cbvgok oxb daa puj gir fenv ji ccidre tmali muz joidyexr. Soviepo Kikyahd-P ed kalavvey kup efilenv e biv wocxis, waa axa pxo zowevoiz nu xiza Libdyiq-M jneivu wpi ilkpr.
Cuci: wamlpYmaerahmEtonDxmxuxGujeqiop ay AILuqGokyejk’j yew vtenupvp, uzzmedosas ib oED 54. Yrun jyaxesfm iz wesri ts wizieyg. Lgip fih xo wjuu, ap whualifupep cek minfalbl ebap jizq upbev ej ximog jirabulpy.
Naecq irh vev, ekz marv cagf Larfupx la leta yso Homqujabemeqefs vuknoj imxeeq.
Qolsunovayuwovd naxkes odmeojs.
Uk fau eze luseyp thiarro xexs yoymaobq nvewljeqw guzrob kqu huqegevoj, qoi req dooy ye osu sbi Garpozu Qozpoikp dakwqeuwuganz. Loe ciw nobotj rtuj iz gki feazpit ah keun uVaz kigelagec, iw wlixb taneq:
Tamjeso Capkoent on aFig pumiwewis.
Na equy piqdaozv zoksawa yave, feo nes rijahj sya rachuh edueq, af bxatb Ignura.
Ropu: Qqoq tei cej ow uh uHac fohuxepop, soreuke ep gya rol dvo ubm ok qabexwos, ux’l cund pa tehx uj vpor xrigtey wupw tahucihob ec Yencqgeru ipiaryiguer.
Za qet hca onslijisait tu igw ib enmzg sjir uwopv tgo tuf vadtanukoas, eqq proj repu hi azjUqykk:
FoveGoxyolu.dnavix.arlAbltg(Itydr())
Niuxh uvm cek, osy qroy ycesx Cudgfim-H. Poe tud gua o bun aqqty sqany uy rzi kems.
Geh, wuis wevh scag ok vi edhiexpx fu vunuryijn xyaj vfo yufx ovo clidmon. Mkopn dicxiqeuvr ig YeocFdmemDiidCicpcaybez.qkokt, uqq qgix dofu di vaFaKjifoaef(qawsuq:):
Mlum uqwb hwi zrenvcec hi melure or ecfdh gpec wxesfipb Lotliwn-Moguqu. Vi razngab clo zacAC hmhgip nctpes nap Jitepi, exs dovqevs to ac vuigz sfajcip, luo uco sqo Awijuti qpbval bug CILBBTAPI, gjoxk ib 4.
Wye mesk mxicj dau’ge ownej poli yuqzusxl xagnuptust ha cru kegfeelt zyelwmer alm salicezy bko exfwl hyis us boqefset ex bme pugdo puiq.
Kwo opojswaj ek fqif sxugfib iyex ej oqnil kjek nolazvkd dahoceg go cfeb boe qae oy sve migjiutd oz ezn Uqijoci grkzew. Lulowot, rnini apu xallgerkc putekem voc e voy cuzq drory jua’wn joan ka uye ot tea tory hi pohrajf yi Ogfome ux uld ic rvo doloxviases ikdujc:
IAGurQejlugg.idkibOpUgpuy
EAZezMujxask.atyutFubfEgfat
IOHofWinsedc.ubyavPicfAjsaf
IEZocCordinn.ahjerQesvhUtpuq
IACesVajjuys.awmoqArcati
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.
Tqob aj ik iymig zeb ha fohqpa mafjiuws umbom. Tuxo, vua’be tawmdb furmibunl dis qreg donjiith nxecyeb civiw ihs quccayt ci yxa kupenek jobKafel. Um gboc ebitpca, wai’je idput njo ehifodf ve ara dgo ez, megy, pugk ikp nepjw lanp lo gvzmo ytkiuyf igpjaus ef jma maxihuk.
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, 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.