You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/SwiftKeys/Documentation.docc/Articles/GettingStarted.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ There are two top-level types in this package: ``KeyCommand`` and ``KeyRecorder`
8
8
9
9
#### KeyCommand
10
10
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`.
12
12
13
13
```swift
14
14
let command =KeyCommand(name: "OpenPreferences")
@@ -79,7 +79,7 @@ In addition to operating on the key command itself, you can perform similar chan
79
79
80
80
``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.
81
81
82
-

82
+

83
83
84
84
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.
Copy file name to clipboardExpand all lines: Sources/SwiftKeys/Documentation.docc/SwiftKeys.md
+71-14Lines changed: 71 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,42 +6,99 @@ A straightforward global key command API for macOS.
6
6
7
7
`SwiftKeys` allows you to create, observe, and record global hotkeys.
8
8
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(_:)``:
10
12
11
13
```swift
12
-
let command =KeyCommand(name: "SomeCommand")
13
-
let recorder =KeyRecorder(keyCommand: command)
14
+
let command =KeyCommand(name: "ToggleMainWindow")
14
15
15
16
command.observe(.keyDown) {
16
-
print("DOWN")
17
+
myCustomKeyDownAction()
17
18
}
19
+
18
20
command.observe(.keyUp) {
19
-
print("UP")
21
+
myCustomKeyUpAction()
22
+
}
23
+
24
+
command.observe(.doubleTap(0.2)) {
25
+
myCustomDoubleTapAction()
20
26
}
21
27
```
22
28
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
24
36
37
+
```swift
38
+
structSettingsView: View {
39
+
var body: some View {
40
+
KeyRecorderView(name: "ToggleMainWindow")
41
+
}
42
+
}
43
+
```
44
+
45
+
#### Cocoa
46
+
47
+
```swift
48
+
classSettingsViewController: NSViewController {
49
+
let recorder =KeyRecorder(name: "ToggleMainWindow")
50
+
51
+
overridefuncviewDidLoad() {
52
+
super.viewDidLoad()
53
+
view.addSubview(recorder)
54
+
}
55
+
}
56
+
```
57
+
58
+
The result should look something like this:
59
+
60
+

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.
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.
0 commit comments