Code Monkey home page Code Monkey logo

keyboardshortcuts's Introduction

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.

This package is still in its early days. 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

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
	func applicationDidFinishLaunching(_ notification: Notification) {
		KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) {
			// The user pressed the keyboard shortcut for “unicorn mode”!
			self.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.

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.

Related

keyboardshortcuts's People

Contributors

sindresorhus avatar gpoitch avatar ywolff avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.