[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7102":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":17,"stars30d":18,"stars90d":16,"forks30d":16,"starsTrendScore":16,"compositeScore":19,"rankGlobal":10,"rankLanguage":10,"license":20,"archived":21,"fork":21,"defaultBranch":22,"hasWiki":21,"hasPages":23,"topics":24,"createdAt":10,"pushedAt":10,"updatedAt":34,"readmeContent":35,"aiSummary":36,"trendingCount":16,"starSnapshotCount":16,"syncStatus":37,"lastSyncTime":38,"discoverSource":39},7102,"KeyboardShortcuts","sindresorhus\u002FKeyboardShortcuts","sindresorhus","⌨️ Add user-customizable global keyboard shortcuts (hotkeys) to your macOS app in minutes","https:\u002F\u002Fswiftpackageindex.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fdocumentation\u002Fkeyboardshortcuts\u002Fkeyboardshortcuts",null,"Swift",2636,244,25,5,0,3,17,29.17,"MIT License",false,"main",true,[25,26,27,28,29,30,31,32,33],"carthage","cocoapods","hotkey","keyboard-shortcuts","macos","swift-package","swift-package-manager","swiftui","swiftui-components","2026-06-12 02:01:34","\u003Cdiv align=\"center\">\n\t\u003Cimg width=\"900\" src=\"https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fraw\u002Fmain\u002Flogo-light.png#gh-light-mode-only\" alt=\"KeyboardShortcuts\">\n\t\u003Cimg width=\"900\" src=\"https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fraw\u002Fmain\u002Flogo-dark.png#gh-dark-mode-only\" alt=\"KeyboardShortcuts\">\n\t\u003Cbr>\n\u003C\u002Fdiv>\n\nThis package lets you add support for user-customizable global keyboard shortcuts to your macOS app in minutes. It's fully sandboxed and Mac App Store compatible. And it's used in production by [Dato](https:\u002F\u002Fsindresorhus.com\u002Fdato), [Jiffy](https:\u002F\u002Fsindresorhus.com\u002Fjiffy), [Plash](https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FPlash), and [Lungo](https:\u002F\u002Fsindresorhus.com\u002Flungo).\n\nI'm happy to accept more configurability and features. PRs welcome! What you see here is just what I needed for my own apps.\n\n\u003Cimg src=\"https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fraw\u002Fmain\u002Fscreenshot.png\" width=\"532\">\n\n## Requirements\n\nmacOS 10.15+\n\n## Install\n\nAdd `https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts` in the [“Swift Package Manager” tab in Xcode](https:\u002F\u002Fdeveloper.apple.com\u002Fdocumentation\u002Fxcode\u002Fadding_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\u002F\u002F …\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:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FPlash\u002Fblob\u002Fb348a62645a873abba8dc11ff0fb8fe423419411\u002FPlash\u002FPreferencesView.swift#L121-L130) in my Plash app.\n\n#### Cocoa\n\nUsing [`KeyboardShortcuts.RecorderCocoa`](Sources\u002FKeyboardShortcuts\u002FRecorderCocoa.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](\u002FSources\u002FKeyboardShortcuts\u002FLocalization). PRs welcome for more!\n\n1. Fork the repo.\n2. Create a directory that has a name that uses an [ISO 639-1](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FList_of_ISO_639-1_codes) language code and optional designators, followed by the `.lproj` suffix. [More here.](https:\u002F\u002Fdeveloper.apple.com\u002Fdocumentation\u002Fswift_packages\u002Flocalizing_package_resources)\n3. Create a file named `Localizable.strings` under the new language directory and then copy the contents of `KeyboardShortcuts\u002FLocalization\u002Fen.lproj\u002FLocalizable.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:\u002F\u002Fswiftpackageindex.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fdocumentation\u002Fkeyboardshortcuts\u002Fkeyboardshortcuts)\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. -->\n\nSee [`NSMenuItem#setShortcut`](https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fblob\u002F0dcedd56994d871f243f3d9c76590bfd9f8aba69\u002FSources\u002FKeyboardShortcuts\u002FNSMenuItem%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 `KeyboardShortcuts.Name`'s dynamically and store them yourself. You can see this in action in the example project.\n\n#### Hard-coded keyboard shortcuts\n\nIf you need a hard-coded global shortcut, you can listen to a `KeyboardShortcuts.Shortcut` directly.\n\n```swift\nimport KeyboardShortcuts\n\nlet shortcut = KeyboardShortcuts.Shortcut(.a, modifiers: [.command])\n\nTask {\n\tfor await eventType in KeyboardShortcuts.events(for: shortcut) where eventType == .keyUp {\n\t\t\u002F\u002F Do something.\n\t}\n}\n```\n\nPrefer user-customizable shortcuts whenever possible.\n\n#### Repeat while held\n\nIf you need repeated actions while the shortcut is held, use `repeatingKeyDownEvents(for:)`. It emits once on initial press, then repeats using the system key repeat settings. (macOS 13+)\n\n```swift\nimport KeyboardShortcuts\n\nTask {\n\tfor await _ in KeyboardShortcuts.repeatingKeyDownEvents(for: .moveSelectionDown) {\n\t\t\u002F\u002F Move to the next item.\n\t}\n}\n```\n\n#### Initial keyboard shortcuts\n\nSetting an initial 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\", initial: .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\u002F\u002F …\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#### Convert modifier flags to symbols\n\nYou can get a symbolic representation of modifier flags like this:\n\n```swift\nimport KeyboardShortcuts\n\nlet modifiers = NSEvent.ModifierFlags([.command, .shift])\nprint(modifiers.ks_symbolicRepresentation)\n\u002F\u002F=> \"⇧⌘\"\n\n\u002F\u002F Also works with shortcuts:\nif let shortcut = KeyboardShortcuts.getShortcut(for: .toggleUnicornMode) {\n\tprint(shortcut.modifiers.ks_symbolicRepresentation)\n\t\u002F\u002F=> \"⌘⌥\"\n}\n```\n\n## FAQ\n\n#### How is it different from [`MASShortcut`](https:\u002F\u002Fgithub.com\u002Fshpakovski\u002FMASShortcut)?\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:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FKeyboardShortcuts\u002Fissues\u002F1) (e.g. menu bar apps).\n\n`MASShortcut`:\n- More mature.\n- More localizations.\n\n\u003C!-- For migration recipes, see the [migration guide](Sources\u002FKeyboardShortcuts\u002FKeyboardShortcuts.docc\u002FMigration.md). -->\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#### Can I use this for customizable in-app keyboard shortcuts?\n\nYes. Use `KeyboardShortcuts.Recorder` with a `Binding\u003CKeyboardShortcuts.Shortcut?>` to let users record a shortcut without registering any global hotkey, then apply it with `.keyboardShortcut(shortcut?.toSwiftUI)`. The shortcut only fires when your app is focused. To persist it across launches, save it to `UserDefaults` or `@AppStorage` yourself.\n\n```swift\nimport SwiftUI\nimport KeyboardShortcuts\n\nstruct ContentView: View {\n\t@State private var shortcut: KeyboardShortcuts.Shortcut?\n\n\tvar body: some View {\n\t\tVStack {\n\t\t\tKeyboardShortcuts.Recorder(\"Record shortcut\", shortcut: $shortcut)\n\t\t\tButton(\"Perform Action\") {\n\t\t\t\tperformAction()\n\t\t\t}\n\t\t\t.keyboardShortcut(shortcut?.toSwiftUI)\n\t\t}\n\t}\n}\n```\n\n#### Can I use custom storage for shortcuts?\n\nYes. Use `KeyboardShortcuts.Recorder` with `shortcut: Binding\u003CKeyboardShortcuts.Shortcut?>` to read and write shortcuts from your own storage, and use `KeyboardShortcuts.events(for: shortcut)` to listen to those shortcuts.\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:\u002F\u002Fgithub.com\u002Fnhurden\u002FMediaKeyTap).\n\n#### Can I listen to the Caps Lock key?\n\nNo, Caps Lock is a modifier key and cannot be directly listened to using this package's standard event methods. If you need to detect Caps Lock events, you'll need to use lower-level APIs like [`CGEvent.tapCreate`](https:\u002F\u002Fdeveloper.apple.com\u002Fdocumentation\u002Fcoregraphics\u002Fcgevent\u002F1454426-tapcreate).\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:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FDefaults) - Swifty and modern UserDefaults\n- [LaunchAtLogin](https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002FLaunchAtLogin) - Add \"Launch at Login\" functionality to your macOS app\n- [More…](https:\u002F\u002Fgithub.com\u002Fsearch?q=user%3Asindresorhus+language%3Aswift+archived%3Afalse&type=repositories)\n","KeyboardShortcuts 是一个用于在 macOS 应用中快速添加用户自定义全局快捷键的 Swift 包。其核心功能包括支持通过 SwiftUI 或 Cocoa 构建界面来让用户选择和设置快捷键，并且能够自动处理与系统或其他应用菜单项冲突的情况。该库完全兼容沙盒环境及 Mac App Store 的要求，使用 `UserDefaults` 来存储配置信息。适用于需要提供高度个性化体验的应用场景，如提高生产力工具、媒体播放器或任何希望增强用户交互性的 macOS 软件。已有多款知名应用程序采用了此方案。",2,"2026-06-11 03:10:32","top_language"]