Skip to content

Commit

Permalink
Document how to get all keyboard shortcuts
Browse files Browse the repository at this point in the history
Fixes #53
Closes #57
  • Loading branch information
sindresorhus committed Jan 23, 2022
1 parent ab6822a commit 9bc72c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/KeyboardShortcuts/Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down

0 comments on commit 9bc72c4

Please sign in to comment.