All Projects → sindresorhus → Keyboardshortcuts

sindresorhus / Keyboardshortcuts

Licence: mit
Add user-customizable global keyboard shortcuts to your macOS app in minutes

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Keyboardshortcuts

Tweetextfield
Lightweight set of text fields with nice animation and functionality. 🚀 Inspired by https://uimovement.com/ui/2524/input-field-help/
Stars: ✭ 421 (-15.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Aksidemenu
Beautiful iOS side menu library with parallax effect. Written in Swift
Stars: ✭ 216 (-56.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Kvkcalendar
A most fully customization calendar and timeline library for iOS 📅
Stars: ✭ 160 (-68%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Coregpx
A library for parsing and creation of GPX location files. Purely Swift.
Stars: ✭ 132 (-73.6%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Corestore
Unleashing the real power of Core Data with the elegance and safety of Swift
Stars: ✭ 3,254 (+550.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Ducttape
📦 KeyPath dynamicMemberLookup based syntax sugar for Swift.
Stars: ✭ 138 (-72.4%)
Mutual labels:  cocoapods, carthage, swift-package-manager
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (-64.6%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Alamofire
Elegant HTTP Networking in Swift
Stars: ✭ 36,896 (+7279.2%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Swiftytexttable
A lightweight library for generating text tables.
Stars: ✭ 252 (-49.6%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Swipycell
Easy to use UITableViewCell implementing swiping to trigger actions.
Stars: ✭ 230 (-54%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Natrium
A pre-build (Swift) script to alter your Xcode project at pre-build-time per environment, build configuration and target.
Stars: ✭ 131 (-73.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Web3.swift
A pure swift Ethereum Web3 library
Stars: ✭ 295 (-41%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Bettersegmentedcontrol
An easy to use, customizable replacement for UISegmentedControl & UISwitch.
Stars: ✭ 1,782 (+256.4%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Cdmarkdownkit
An extensive Swift framework providing simple and customizable markdown parsing.
Stars: ✭ 158 (-68.4%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Randomkit
Random data generation in Swift
Stars: ✭ 1,458 (+191.6%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Multipeer
📱📲 A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices
Stars: ✭ 170 (-66%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Buckets Swift
Swift Collection Data Structures Library
Stars: ✭ 106 (-78.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Swifterswift
A handy collection of more than 500 native Swift extensions to boost your productivity.
Stars: ✭ 10,706 (+2041.2%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Amplitude Ios
Native iOS/tvOS/macOS SDK
Stars: ✭ 216 (-56.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
Stepperview
SwiftUI iOS component for Step Indications.
Stars: ✭ 281 (-43.8%)
Mutual labels:  cocoapods, carthage, swift-package-manager
KeyboardShortcuts

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.

Requirements

macOS 10.11+

Install

Swift Package Manager

Add https://github.com/sindresorhus/KeyboardShortcuts in the “Swift Package Manager” tab in Xcode.

You also need to set the build setting “Other Linker Flags” to -weak_framework Combine to work around this Xcode bug.

Carthage

github "sindresorhus/KeyboardShortcuts"

CocoaPods

pod 'KeyboardShortcuts'

Usage

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.

PreferencesView.swift

import SwiftUI
import KeyboardShortcuts

struct PreferencesView: View {
	var body: some View {
		HStack {
			Text("Toggle Unicorn Mode:")
			KeyboardShortcuts.Recorder(for: .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.

AppDelegate.swift

import Cocoa
import KeyboardShortcuts

@main
final class AppDelegate: NSObject, NSApplicationDelegate {
	func applicationDidFinishLaunching(_ notification: Notification) {
		KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in
			// The user pressed the keyboard shortcut for “unicorn mode”!
			isUnicornMode.toggle()
		}
	}
}

You can also listen to key down with .onKeyDown()

That's all! ✨

You can find a complete example by opening KeyboardShortcuts.xcodeproj and then running the KeyboardShortcutsExample target.

You can also find a real-world example in my Plash app.

Cocoa

Use KeyboardShortcuts.RecorderCocoa instead of KeyboardShortcuts.Recorder.

import Cocoa
import KeyboardShortcuts

final class PreferencesViewController: NSViewController {
	override func loadView() {
		view = NSView()

		let recorder = KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode)
		view.addSubview(recorder)
	}
}

API

See the API docs.

Tips

Show a recorded keyboard shortcut in an NSMenuItem

See NSMenuItem#setShortcut.

Dynamic keyboard shortcuts

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.

Default keyboard shortcuts

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. It’s 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]))
}

FAQ

How is it different from MASShortcut?

This package:

  • Written in Swift with a swifty API.
  • More native-looking UI component.
  • SwiftUI component included.
  • Support for listening to key down, not just key up.
  • Swift Package Manager support.
  • Connect a shortcut to an NSMenuItem.

MASShortcut:

  • More mature.
  • More features.
  • Localized.

How is it different from 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.

Why is this package importing 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.

Does this package cause any permission dialogs?

No.

How can I add an app-specific keyboard shortcut that is only active when the app is?

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.

Related

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].