{"id":13694649,"url":"https://github.com/emcro/SwiftUI-Keyboard-Demo","last_synced_at":"2025-05-03T04:30:42.531Z","repository":{"id":147310873,"uuid":"262095557","full_name":"emcro/SwiftUI-Keyboard-Demo","owner":"emcro","description":"How to add Keyboard Shortcuts to any SwiftUI App with UIKeyCommand","archived":false,"fork":false,"pushed_at":"2020-05-08T17:36:21.000Z","size":25,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-12T21:39:02.619Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emcro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-05-07T15:59:25.000Z","updated_at":"2024-06-16T13:08:04.000Z","dependencies_parsed_at":"2024-04-12T23:45:26.271Z","dependency_job_id":"7386dd9f-79d3-4866-a4f5-36a8e48a0807","html_url":"https://github.com/emcro/SwiftUI-Keyboard-Demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emcro%2FSwiftUI-Keyboard-Demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emcro%2FSwiftUI-Keyboard-Demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emcro%2FSwiftUI-Keyboard-Demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emcro%2FSwiftUI-Keyboard-Demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emcro","download_url":"https://codeload.github.com/emcro/SwiftUI-Keyboard-Demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252144360,"owners_count":21701397,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T17:01:36.457Z","updated_at":"2025-05-03T04:30:41.907Z","avatar_url":"https://github.com/emcro.png","language":"Swift","funding_links":[],"categories":["Misc"],"sub_categories":["SwiftUI"],"readme":"# SwiftUI-Keyboard-Demo\n\nThis tiny project was built to show how simple it is to add keyboard shortcuts (with UIKeyCommand) to any SwiftUI app.\nAfter implementing the main parts below, you'll have the hold-down-⌘ button work in all of the views where you want to\nsupport it, listing out every keyboard shortcut available to the user. The user expects it, don't let them down!\n\n![⌘ Key Preview](http://cln.sh/lpJS+)\n\n## Full working example for basic tab switching\n\nIf all you want to do is add add Cmd-1, Cmd-2, etc keyboard shortcuts to active different tabs, literally all you need is the following:\n\n### AppDelegate\n* Add `override var keyCommands` which returns an array of `UIKeyCommand`, and a handler to handle those using NotificationCenter\n\n```\nimport UIKit\n\n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n    override var keyCommands: [UIKeyCommand]? {\n        return [\n            UIKeyCommand(title: \"First Tab\", action: #selector(handleKeyCommand(sender:)), input: \"1\", modifierFlags: .command, propertyList: 1),\n            \n            UIKeyCommand(title: \"Second Tab\", action: #selector(handleKeyCommand(sender:)), input: \"2\", modifierFlags: .command, propertyList: 2)\n        ]\n    }\n    \n    @objc func handleKeyCommand(sender: UIKeyCommand) {\n        if let tabTag = sender.propertyList as? Int {\n            NotificationCenter.default.post(name: .init(\"switchTabs\"), object: tabTag)\n        }\n    }\n}\n```\n\n### ContentView\n* Add `onReceive` to handle the posted Notification and use the `selection` binding to jump to the correct tab\n\n```\nimport SwiftUI\n\nstruct ContentView: View {\n    @State private var selection = 1\n    \n    var body: some View {\n        TabView(selection: $selection){\n            Text(\"First View\")\n                .tabItem {\n                    VStack {\n                        Image(\"first\")\n                        Text(\"First\")\n                    }\n            }\n            .tag(1)\n            \n            Text(\"Second View\")\n                .tabItem {\n                    VStack {\n                        Image(\"second\")\n                        Text(\"Second\")\n                    }\n            }\n            .tag(2)\n        }\n        .onReceive(NotificationCenter.default.publisher(for: Notification.Name(\"switchTabs\"))) { notification in\n            if let tabTag = notification.object as? Int {\n                self.selection = tabTag\n            }\n        }\n    }\n}\n```\n\n----\n\nIn the full demo, I also show how you can use an `ObservableObject` and state for a more complicated example, to allow for opening and closing modals, using hidden keyboard shortcuts, etc, but the basics are the same. The app will always look \nto the `keyCommands` defined in the AppDelegate, so based on the user's current view, you would simply return a \ndifferent array of `UIKeyCommand`s.\n\nThere's a more thorough walkthrough that I'll be writing up on Medium soon, but I wanted to get this out there as soon as \npossible as I couldn't find *any* solution through Google or asking some of the great SwiftUI fans on Twitter and \nelsewhere for a clean way to do this. I purposely provided a few different methods to help you find the best way to \nintegrate these keyboard shortcuts in your apps, and urge you to *please* add them ASAP. It's just behind adding \naccessibility to an app in my view, especially with the amazing new Magic Keyboard for iPad Pro, you likely have a lot \nmore users now who will be looking for keyboard shortcuts in your iPad app.\n\n## Invisible Keyboard Shortcuts\nOne more cool thing in here is adding \"invisible\" shortcuts by leaving the title of your `UIKeyCommand` empty, \nas you can see implemented for modals. *Please* add support for the universal methods to close any kind of modal \nand make the iPad world a better place, ⌘-W and Esc (which will also give you ⌘-. for free, as it sends Esc when used):\n\n```\nUIKeyCommand(title: \"\", action: #selector(handleKeyCommand(sender:)), input: UIKeyCommand.inputEscape, propertyList: \"closeModal\"),\nUIKeyCommand(title: \"\", action: #selector(handleKeyCommand(sender:)), input: \"W\", modifierFlags: .command, propertyList: \"closeModal\")\n```\n\n## Users-Will-Love-You Keyboard Shortcut\nI also added in the backtick, as that is where Esc would normally be on a US keyboard, after seeing \nthe always-awesome-and-funny [Christian Selig](https://twitter.com/ChristianSelig) add it into his equally awesome Apollo app \nfor Reddit and though it was just too damn clever not use myself in my app:\n\n```\nUIKeyCommand(title: \"Close\", action: #selector(handleKeyCommand(sender:)), input: \"`\", propertyList: \"closeModal\")\n\n```\n\n## Try it out in the App Store with CardPointers\nSpeaking of which, shameless plug ahead: if you have a credit card or you're looking for your first one(s) to help get the \nmost cash back or points/miles to make traveling ridiculously cheap/free, or if you just want to play with a free live \napp in the App Store that's using these exact same keyboard shortcut techniques, grab \n[CardPointers](https://apps.apple.com/us/app/cardpointers/id1472875808) from the App Store, you'll be happy you did.\n\n## Contact/PRs/Issues\nPRs are most welcome and I'll do my best to answer any questions; best way to reach me is through Twitter \n[@emcro](https://twitter.com/emcro).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femcro%2FSwiftUI-Keyboard-Demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femcro%2FSwiftUI-Keyboard-Demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femcro%2FSwiftUI-Keyboard-Demo/lists"}