{"id":13465799,"url":"https://github.com/sindresorhus/KeyboardShortcuts","last_synced_at":"2025-03-25T16:32:46.473Z","repository":{"id":38374668,"uuid":"262082775","full_name":"sindresorhus/KeyboardShortcuts","owner":"sindresorhus","description":"⌨️ Add user-customizable global keyboard shortcuts (hotkeys) to your macOS app in minutes","archived":false,"fork":false,"pushed_at":"2025-03-07T09:59:10.000Z","size":1480,"stargazers_count":2163,"open_issues_count":27,"forks_count":200,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-03-19T17:39:02.036Z","etag":null,"topics":["carthage","cocoapods","hotkey","keyboard-shortcuts","macos","swift-package","swift-package-manager","swiftui","swiftui-components"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/sindresorhus/KeyboardShortcuts/documentation/keyboardshortcuts/keyboardshortcuts","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","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,"publiccode":null,"codemeta":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2020-05-07T15:06:18.000Z","updated_at":"2025-03-19T05:17:30.000Z","dependencies_parsed_at":"2023-02-10T08:31:48.717Z","dependency_job_id":"994fde5e-a12d-40b2-ba08-1805ba95ac96","html_url":"https://github.com/sindresorhus/KeyboardShortcuts","commit_stats":{"total_commits":131,"total_committers":33,"mean_commits":"3.9696969696969697","dds":"0.35114503816793896","last_synced_commit":"db506816e7a088908d76d26c308d5405e65ad079"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2FKeyboardShortcuts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2FKeyboardShortcuts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2FKeyboardShortcuts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2FKeyboardShortcuts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/KeyboardShortcuts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245305365,"owners_count":20593701,"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":["carthage","cocoapods","hotkey","keyboard-shortcuts","macos","swift-package","swift-package-manager","swiftui","swiftui-components"],"created_at":"2024-07-31T15:00:35.627Z","updated_at":"2025-03-25T16:32:46.444Z","avatar_url":"https://github.com/sindresorhus.png","language":"Swift","readme":"\u003cdiv align=\"center\"\u003e\n\t\u003cimg width=\"900\" src=\"https://github.com/sindresorhus/KeyboardShortcuts/raw/main/logo-light.png#gh-light-mode-only\" alt=\"KeyboardShortcuts\"\u003e\n\t\u003cimg width=\"900\" src=\"https://github.com/sindresorhus/KeyboardShortcuts/raw/main/logo-dark.png#gh-dark-mode-only\" alt=\"KeyboardShortcuts\"\u003e\n\t\u003cbr\u003e\n\u003c/div\u003e\n\nThis package lets you add support for user-customizable global keyboard shortcuts to your macOS app in minutes. It's fully sandbox and Mac App Store compatible. And it's used in production by [Dato](https://sindresorhus.com/dato), [Jiffy](https://sindresorhus.com/jiffy), [Plash](https://github.com/sindresorhus/Plash), and [Lungo](https://sindresorhus.com/lungo).\n\nI'm happy to accept more configurability and features. PR welcome! What you see here is just what I needed for my own apps.\n\n\u003cimg src=\"https://github.com/sindresorhus/KeyboardShortcuts/raw/main/screenshot.png\" width=\"532\"\u003e\n\n## Requirements\n\nmacOS 10.15+\n\n## Install\n\nAdd `https://github.com/sindresorhus/KeyboardShortcuts` in the [“Swift Package Manager” tab in Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app).\n\n## Usage\n\nFirst, register a name for the keyboard shortcut.\n\n`Constants.swift`\n\n```swift\nimport KeyboardShortcuts\n\nextension KeyboardShortcuts.Name {\n\tstatic let toggleUnicornMode = Self(\"toggleUnicornMode\")\n}\n```\n\nYou can then refer to this strongly-typed name in other places.\n\nYou will want to make a view where the user can choose a keyboard shortcut.\n\n`SettingsScreen.swift`\n\n```swift\nimport SwiftUI\nimport KeyboardShortcuts\n\nstruct SettingsScreen: View {\n\tvar body: some View {\n\t\tForm {\n\t\t\tKeyboardShortcuts.Recorder(\"Toggle Unicorn Mode:\", name: .toggleUnicornMode)\n\t\t}\n\t}\n}\n```\n\n*There's also [support for Cocoa](#cocoa) instead of SwiftUI.*\n\n`KeyboardShortcuts.Recorder` takes care of storing the keyboard shortcut in `UserDefaults` and also warning the user if the chosen keyboard shortcut is already used by the system or the app's main menu.\n\nAdd a listener for when the user presses their chosen keyboard shortcut.\n\n`App.swift`\n\n```swift\nimport SwiftUI\nimport KeyboardShortcuts\n\n@main\nstruct YourApp: App {\n\t@State private var appState = AppState()\n\n\tvar body: some Scene {\n\t\tWindowGroup {\n\t\t\t// …\n\t\t}\n\t\tSettings {\n\t\t\tSettingsScreen()\n\t\t}\n\t}\n}\n\n@MainActor\n@Observable\nfinal class AppState {\n\tinit() {\n\t\tKeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in\n\t\t\tisUnicornMode.toggle()\n\t\t}\n\t}\n}\n```\n\n*You can also listen to key down with `.onKeyDown()`*\n\n**That's all! ✨**\n\nYou can find a complete example in the “Example” directory.\n\nYou can also find a [real-world example](https://github.com/sindresorhus/Plash/blob/b348a62645a873abba8dc11ff0fb8fe423419411/Plash/PreferencesView.swift#L121-L130) in my Plash app.\n\n#### Cocoa\n\nUsing [`KeyboardShortcuts.RecorderCocoa`](Sources/KeyboardShortcuts/RecorderCocoa.swift) instead of `KeyboardShortcuts.Recorder`:\n\n```swift\nimport AppKit\nimport KeyboardShortcuts\n\nfinal class SettingsViewController: NSViewController {\n\toverride func loadView() {\n\t\tview = NSView()\n\n\t\tlet recorder = KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode)\n\t\tview.addSubview(recorder)\n\t}\n}\n```\n\n## Localization\n\nThis package supports [localizations](/Sources/KeyboardShortcuts/Localization). PR welcome for more!\n\n1. Fork the repo.\n2. Create a directory that has a name that uses an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code and optional designators, followed by the `.lproj` suffix. [More here.](https://developer.apple.com/documentation/swift_packages/localizing_package_resources)\n3. Create a file named `Localizable.strings` under the new language directory and then copy the contents of `KeyboardShortcuts/Localization/en.lproj/Localizable.strings` to the new file that you just created.\n4. Localize and make sure to review your localization multiple times. Check for typos.\n5. Try to find someone that speaks your language to review the translation.\n6. Submit a PR.\n\n## API\n\n[See the API docs.](https://swiftpackageindex.com/sindresorhus/KeyboardShortcuts/documentation/keyboardshortcuts/keyboardshortcuts)\n\n## Tips\n\n#### Show a recorded keyboard shortcut in an `NSMenuItem`\n\n\u003c!-- TODO: Link to the docs instead when DocC supports showing type extensions. --\u003e\n\nSee [`NSMenuItem#setShortcut`](https://github.com/sindresorhus/KeyboardShortcuts/blob/0dcedd56994d871f243f3d9c76590bfd9f8aba69/Sources/KeyboardShortcuts/NSMenuItem%2B%2B.swift#L14-L41).\n\n#### Dynamic keyboard shortcuts\n\nYour app might need to support keyboard shortcuts for user-defined actions. Normally, you would statically register the keyboard shortcuts upfront in `extension KeyboardShortcuts.Name {}`. However, this is not a requirement. It's only for convenience so that you can use dot-syntax when calling various APIs (for example, `.onKeyDown(.unicornMode) {}`). You can create `KeyboardShortcut.Name`'s dynamically and store them yourself. You can see this in action in the example project.\n\n#### Default keyboard shortcuts\n\nSetting a default keyboard shortcut can be useful if you're migrating from a different package or just making something for yourself. However, please do not set this for a publicly distributed app. Users find it annoying when random apps steal their existing keyboard shortcuts. It’s generally better to show a welcome screen on the first app launch that lets the user set the shortcut.\n\n```swift\nimport KeyboardShortcuts\n\nextension KeyboardShortcuts.Name {\n\tstatic let toggleUnicornMode = Self(\"toggleUnicornMode\", default: .init(.k, modifiers: [.command, .option]))\n}\n```\n\n#### Get all keyboard shortcuts\n\nTo get all the keyboard shortcut `Name`'s, conform `KeyboardShortcuts.Name` to `CaseIterable`.\n\n```swift\nimport KeyboardShortcuts\n\nextension KeyboardShortcuts.Name {\n\tstatic let foo = Self(\"foo\")\n\tstatic let bar = Self(\"bar\")\n}\n\nextension KeyboardShortcuts.Name: CaseIterable {\n\tpublic static let allCases: [Self] = [\n\t\t.foo,\n\t\t.bar\n\t]\n}\n\n// …\n\nprint(KeyboardShortcuts.Name.allCases)\n```\n\nAnd to get all the `Name`'s with a set keyboard shortcut:\n\n```swift\nprint(KeyboardShortcuts.Name.allCases.filter { $0.shortcut != nil })\n```\n\n## FAQ\n\n#### How is it different from [`MASShortcut`](https://github.com/shpakovski/MASShortcut)?\n\nThis package:\n- Written in Swift with a swifty API.\n- More native-looking UI component.\n- SwiftUI component included.\n- Support for listening to key down, not just key up.\n- Swift Package Manager support.\n- Connect a shortcut to an `NSMenuItem`.\n- Works when [`NSMenu` is open](https://github.com/sindresorhus/KeyboardShortcuts/issues/1) (e.g. menu bar apps).\n\n`MASShortcut`:\n- More mature.\n- More localizations.\n\n#### How is it different from [`HotKey`](https://github.com/soffes/HotKey)?\n\n`HotKey` is good for adding hard-coded keyboard shortcuts, but it doesn't provide any UI component for the user to choose their own keyboard shortcuts.\n\n#### Why is this package importing `Carbon`? Isn't that deprecated?\n\nMost of the Carbon APIs were deprecated years ago, but there are some left that Apple never shipped modern replacements for. This includes registering global keyboard shortcuts. However, you should not need to worry about this. Apple will for sure ship new APIs before deprecating the Carbon APIs used here.\n\n#### Does this package cause any permission dialogs?\n\nNo.\n\n#### How can I add an app-specific keyboard shortcut that is only active when the app is?\n\nThat is outside the scope of this package. You can either use [`NSEvent.addLocalMonitorForEvents`](https://developer.apple.com/documentation/appkit/nsevent/1534971-addlocalmonitorforevents), [`NSMenuItem` with keyboard shortcut](https://developer.apple.com/documentation/appkit/nsmenuitem/2880316-allowskeyequivalentwhenhidden) (it can even be hidden), or SwiftUI's [`View#keyboardShortcut()` modifier](https://developer.apple.com/documentation/swiftui/form/keyboardshortcut(_:)).\n\n#### Does it support media keys?\n\nNo, since it would not work for sandboxed apps. If your app is not sandboxed, you can use [`MediaKeyTap`](https://github.com/nhurden/MediaKeyTap).\n\n#### Can you support CocoaPods or Carthage?\n\nNo. However, there is nothing stopping you from using Swift Package Manager for just this package even if you normally use CocoaPods or Carthage.\n\n## Related\n\n- [Defaults](https://github.com/sindresorhus/Defaults) - Swifty and modern UserDefaults\n- [LaunchAtLogin](https://github.com/sindresorhus/LaunchAtLogin) - Add \"Launch at Login\" functionality to your macOS app\n- [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift+archived%3Afalse\u0026type=repositories)\n","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Libs","Swift","Samples","快捷键","Keyboard [🔝](#readme)","🛠 Examples","UI","Tech Stack"],"sub_categories":["Keyboard","Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2FKeyboardShortcuts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2FKeyboardShortcuts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2FKeyboardShortcuts/lists"}