Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Robotjs | 11,434 | 455 | 240 | 10 months ago | 34 | December 08, 2019 | 166 | mit | C | |
Node.js Desktop Automation. | ||||||||||
Hotkeys Js | 6,122 | 609 | 365 | 15 days ago | 91 | August 02, 2023 | 125 | mit | JavaScript | |
➷ A robust Javascript library for capturing keyboard input. It has no dependencies. | ||||||||||
Keyboard | 3,401 | 142 | 309 | 4 months ago | 35 | March 27, 2018 | 362 | mit | Python | |
Hook and simulate global keyboard events on Windows and Linux. | ||||||||||
Hotkey | 2,864 | 2 | 7 | 22 days ago | 32 | July 21, 2022 | 11 | mit | JavaScript | |
Trigger an action on an element with a keyboard shortcut. | ||||||||||
React Hotkeys Hook | 2,138 | 6 | 240 | 10 hours ago | 100 | July 04, 2023 | 35 | mit | TypeScript | |
React hook for using keyboard shortcuts in components. | ||||||||||
Angular Hotkeys | 1,704 | 218 | 14 | 4 years ago | 17 | February 18, 2016 | 106 | mit | JavaScript | |
Configuration-centric keyboard shortcuts for your Angular apps. | ||||||||||
Keyboardshortcuts | 1,475 | 15 days ago | 11 | February 21, 2021 | 17 | mit | Swift | |||
⌨️ Add user-customizable global keyboard shortcuts (hotkeys) to your macOS app in minutes | ||||||||||
Ninja Keys | 1,406 | 7 | 4 months ago | 22 | July 01, 2022 | 26 | mit | TypeScript | ||
Keyboard shortcuts interface for your website. Working with static HTML, Vanilla JS, Vue, React, Svelte. | ||||||||||
Win Vind | 1,207 | 2 months ago | 49 | mit | C++ | |||||
You can operate Windows with key bindings like Vim. | ||||||||||
Capslock Plus | 998 | a month ago | 53 | gpl-2.0 | AutoHotkey | |||||
An efficiency tool that provides various functions by enhancing the Caps Lock key into a modifier key. |
This 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, Jiffy, Plash, and Lungo.
I'm happy to accept more configurability and features. PR welcome! What you see here is just what I needed for my own apps.
macOS 10.13+
Add https://github.com/sindresorhus/KeyboardShortcuts
in the Swift Package Manager tab in Xcode.
First, register a name for the keyboard shortcut.
Constants.swift
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let toggleUnicornMode = Self("toggleUnicornMode")
}
You can then refer to this strongly-typed name in other places.
You will want to make a view where the user can choose a keyboard shortcut.
SettingsScreen.swift
import SwiftUI
import KeyboardShortcuts
struct SettingsScreen: View {
var body: some View {
Form {
KeyboardShortcuts.Recorder("Toggle Unicorn Mode:", name: .toggleUnicornMode)
}
}
}
There's also support for Cocoa instead of SwiftUI.
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.
Add a listener for when the user presses their chosen keyboard shortcut.
App.swift
import SwiftUI
import KeyboardShortcuts
@main
struct YourApp: App {
@StateObject private var appState = AppState()
var body: some Scene {
WindowGroup {
//
}
Settings {
SettingsScreen()
}
}
}
@MainActor
final class AppState: ObservableObject {
init() {
KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in
isUnicornMode.toggle()
}
}
}
You can also listen to key down with .onKeyDown()
**That's all! **
You can find a complete example in the Example directory.
You can also find a real-world example in my Plash app.
Using KeyboardShortcuts.RecorderCocoa
instead of KeyboardShortcuts.Recorder
:
import Cocoa
import KeyboardShortcuts
final class SettingsViewController: NSViewController {
override func loadView() {
view = NSView()
let recorder = KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode)
view.addSubview(recorder)
}
}
This package supports localizations. PR welcome for more!
.lproj
suffix. More here.
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.NSMenuItem
Your 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.
Setting 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. Its generally better to show a welcome screen on the first app launch that lets the user set the shortcut.
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let toggleUnicornMode = Self("toggleUnicornMode", default: .init(.k, modifiers: [.command, .option]))
}
To get all the keyboard shortcut Name
's, conform KeyboardShortcuts.Name
to CaseIterable
.
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let foo = Self("foo")
static let bar = Self("bar")
}
extension KeyboardShortcuts.Name: CaseIterable {
public static let allCases: [Self] = [
.foo,
.bar
]
}
//
print(KeyboardShortcuts.Name.allCases)
And to get all the Name
's with a set keyboard shortcut:
print(KeyboardShortcuts.Name.allCases.filter { $0.shortcut != nil })
MASShortcut
?This package:
NSMenuItem
.NSMenu
is open (e.g. menu bar apps).MASShortcut
:
HotKey
?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.
Carbon
? Isn't that deprecated?Most 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.
No.
That is outside the scope of this package. You can either use NSEvent.addLocalMonitorForEvents
, NSMenuItem
with keyboard shortcut (it can even be hidden), or SwiftUI's View#keyboardShortcut()
modifier.
No, since it would not work for sandboxed apps. If your app is not sandboxed, you can use MediaKeyTap
.
No. However, there is nothing stopping you from using Swift Package Manager for just this package even if you normally use CocoaPods or Carthage.