Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soffes/HotKey
Simple global shortcuts in macOS
https://github.com/soffes/HotKey
appkit carbon carthage keyboard macos shortcut swift
Last synced: about 2 months ago
JSON representation
Simple global shortcuts in macOS
- Host: GitHub
- URL: https://github.com/soffes/HotKey
- Owner: soffes
- License: mit
- Created: 2017-07-21T11:30:55.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-06-07T06:26:24.000Z (7 months ago)
- Last Synced: 2024-10-01T18:17:44.836Z (3 months ago)
- Topics: appkit, carbon, carthage, keyboard, macos, shortcut, swift
- Language: Swift
- Size: 74.2 KB
- Stars: 926
- Watchers: 11
- Forks: 82
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Support: Support/HotKey.h
Awesome Lists containing this project
- awesome-starred-test - soffes/HotKey - Simple global shortcuts in macOS (Swift)
README
# HotKey
[![Version](https://img.shields.io/github/release/soffes/HotKey.svg)](https://github.com/soffes/HotKey/releases)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods compatible](https://img.shields.io/cocoapods/v/HotKey.svg)](https://cocoapods.org/pods/HotKey)Simple global shortcuts in macOS. HotKey wraps the Carbon APIs for dealing with global hot keys to make it easy to use in Swift.
## Installation
### Swift Package Manager
For installation with [Swift Package Manager](https://github.com/apple/swift-package-manager), simply add the following to your `Package.swift`:
``` swift
.package(url: "https://github.com/soffes/HotKey", from: "0.2.0")
```### Carthage
For installation with [Carthage](https://github.com/carthage/carthage), simply add the following to your `Cartfile`:
``` ruby
github "soffes/HotKey"
```### CocoaPods
For installation with [CocoaPods](http://cocoapods.org), simply add the following to your `Podfile`:
```ruby
pod 'HotKey'
```## Usage
Simply initialize a `HotKey` with a key and modifiers:
```swift
// Setup hot key for ⌥⌘R
let hotKey = HotKey(key: .r, modifiers: [.command, .option])
```This is a convenice initializer that creates a `KeyCombo` for you. You can also initialize with a Carbon key code and Carbon modifier flags if you’re feeling old school.
Now you can set the `keyDownHandler` and get callbacks for when your hot key is pressed:
```swift
hotKey.keyDownHandler = {
print("Pressed at \(Date())")
}
```You can also implement `hotKey.keyUpHandler` if you’d like.
You don’t need to think about when handlers are registered and unregistered. This all happens automatically based on the `HotKey` object’s lifecycle.
## Thanks
HotKey was heavily inspired by PTHotKey.