Document how to get all keyboard shortcuts

Fixes #53
Closes #57
This commit is contained in:
Sindre Sorhus 2022-01-23 12:39:21 +07:00
parent ab6822ade2
commit 9bc72c441c
2 changed files with 35 additions and 0 deletions

View File

@ -20,6 +20,11 @@ extension KeyboardShortcuts {
public let rawValue: String
public let defaultShortcut: Shortcut?
/**
Get the keyboard shortcut assigned to the name.
*/
public var shortcut: Shortcut? { KeyboardShortcuts.getShortcut(for: self) }
/**
- Parameter name: Name of the shortcut.
- Parameter default: Optional default key combination. Do not set this unless it's essential. 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.

View File

@ -181,6 +181,36 @@ extension KeyboardShortcuts.Name {
}
```
#### Get all keyboard shortcuts
To get all the keyboard shortcut `Name`'s, conform `KeyboardShortcuts.Name` to `CaseIterable`.
```swift
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:
```swift
print(KeyboardShortcuts.Name.allCases.filter { $0.shortcut != nil })
```
## FAQ
#### How is it different from [`MASShortcut`](https://github.com/shpakovski/MASShortcut)?