Skip to content

Commit d178b6e

Browse files
committed
Update documentation
1 parent 6964a2b commit d178b6e

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

Sources/SwiftKeys/Documentation.docc/Articles/GettingStarted.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ There are two top-level types in this package: ``KeyCommand`` and ``KeyRecorder`
88

99
#### KeyCommand
1010

11-
``KeyCommand`` is quite powerful, yet simple to understand. To create one, all you need is a name, which will be used to store the command in `UserDefaults`.
11+
To create a ``KeyCommand``, all you need is a name, which will be used to store the command in `UserDefaults`.
1212

1313
```swift
1414
let command = KeyCommand(name: "OpenPreferences")
@@ -79,7 +79,7 @@ In addition to operating on the key command itself, you can perform similar chan
7979

8080
``KeyRecorder`` is a subclass of `NSControl` that enables you to record new keys and modifiers for a key command. Passing a command into ``KeyRecorder/init(keyCommand:)`` creates a key recorder whose state is bound to that command. You can also create a key recorder using ``KeyRecorder/init(name:)``, a convenience initializer which automatically creates a key command based on the name you provide.
8181

82-
![A window containing a KeyRecorder.](recorder-window.png)
82+
![A window containing a KeyRecorder.](recorder-window)
8383

8484
Using a key recorder is extremely simple. Clicking inside puts it into "recording" mode, where it awaits a key-down message. As soon as a key combination is pressed, the recorder updates its key command and enters "idle" mode. In idle mode, a "clear" button appears, which resets the key command to an empty state.
8585

Sources/SwiftKeys/Documentation.docc/SwiftKeys.md

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,99 @@ A straightforward global key command API for macOS.
66

77
`SwiftKeys` allows you to create, observe, and record global hotkeys.
88

9-
Start by creating an instance of ``KeyCommand``. Then, use it to initialize a ``KeyRecorder`` instance. The key recorder's state is bound to the key command, so when it records a new key combination, the command will be updated. You can also observe the command and perform actions on both key-down and key-up.
9+
### Creating and Observing
10+
11+
Start by creating an instance of ``KeyCommand``. Observe it, and perform actions on ``KeyCommand/EventType/keyDown``, ``KeyCommand/EventType/keyUp``, and ``KeyCommand/EventType/doubleTap(_:)``:
1012

1113
```swift
12-
let command = KeyCommand(name: "SomeCommand")
13-
let recorder = KeyRecorder(keyCommand: command)
14+
let command = KeyCommand(name: "ToggleMainWindow")
1415

1516
command.observe(.keyDown) {
16-
print("DOWN")
17+
myCustomKeyDownAction()
1718
}
19+
1820
command.observe(.keyUp) {
19-
print("UP")
21+
myCustomKeyUpAction()
22+
}
23+
24+
command.observe(.doubleTap(0.2)) {
25+
myCustomDoubleTapAction()
2026
}
2127
```
2228

23-
For improved type safety, you can create hard-coded key command names that can be referenced across your app.
29+
> ``KeyCommand/EventType/doubleTap(_:)`` allows you to specify a maximum time interval that the two key presses must fall within to be considered a "double-tap".
30+
31+
### Adding a Key Recorder
32+
33+
Use the key command's name to create a key recorder. Then, add it to a view (note the use of ``KeyRecorderView`` for SwiftUI and ``KeyRecorder`` for Cocoa):
34+
35+
#### SwiftUI
2436

37+
```swift
38+
struct SettingsView: View {
39+
var body: some View {
40+
KeyRecorderView(name: "ToggleMainWindow")
41+
}
42+
}
43+
```
44+
45+
#### Cocoa
46+
47+
```swift
48+
class SettingsViewController: NSViewController {
49+
let recorder = KeyRecorder(name: "ToggleMainWindow")
50+
51+
override func viewDidLoad() {
52+
super.viewDidLoad()
53+
view.addSubview(recorder)
54+
}
55+
}
56+
```
57+
58+
The result should look something like this:
59+
60+
![](recorder-window)
61+
62+
The recorder and command will stay synchronized with each other, so when the user records a new key combination, the command will be updated to match the new value.
63+
64+
---
65+
66+
For improved type safety, you can create hard-coded command names that can be referenced across your app.
67+
68+
`Misc.swift`
2569
```swift
2670
extension KeyCommand.Name {
27-
static let showPreferences = Self("ShowPreferences")
71+
static let toggleMainWindow = Self("ToggleMainWindow")
2872
}
29-
let command = KeyCommand(name: .showPreferences)
3073
```
3174

32-
Key commands are automatically stored `UserDefaults`. The name of the command serves as its key. You can provide a custom prefix that will be combined with each name to create the keys.
75+
`AppDelegate.swift`
76+
```swift
77+
let command = KeyCommand(name: .toggleMainWindow)
78+
```
79+
80+
`SettingsView.swift`
81+
```swift
82+
let recorder = KeyRecorder(name: .toggleMainWindow)
83+
```
84+
85+
---
86+
87+
Key commands are automatically stored in the `UserDefaults` system, using their names as keys. It's common for `UserDefaults` keys to be prefixed, or namespaced, according to their corresponding app or subsystem. To that end, `SwiftKeys` lets you provide custom prefixes that can be applied to individual names.
3388

3489
```swift
3590
extension KeyCommand.Name.Prefix {
36-
public override var sharedPrefix: Self {
37-
Self("SK")
38-
}
91+
static let settings = Self("Settings")
92+
static let app = Self("MyGreatApp")
3993
}
4094

4195
extension KeyCommand.Name {
42-
static let showPreferences = Self("ShowPreferences")
96+
// "SettingsOpen" will be the full UserDefaults key.
97+
static let openSettings = Self("Open", prefix: .settings)
98+
99+
// "MyGreatApp_Quit" will be the full UserDefaults key.
100+
static let quitApp = Self("Quit", prefix: .app, separator: "_")
43101
}
44-
// The name above will become "SKShowPreferences" when used as a defaults key.
45102
```
46103

47104
You can find `SwiftKeys` [on GitHub](https://github.com/jordanbaird/SwiftKeys)

0 commit comments

Comments
 (0)