-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jordanbaird/improvements
- Loading branch information
Showing
189 changed files
with
435 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# ``SwiftKeys`` | ||
|
||
A straightforward global hotkey API for macOS. | ||
|
||
## Overview | ||
|
||
``SwiftKeys`` allows you to create, observe, and record global hotkeys in the form of the | ||
``KeyEvent`` type. | ||
|
||
Start by creating an instance of ``KeyEvent``. Then, use it to initialize a ``KeyRecorder`` | ||
instance. The recorder will stay synchronized with the key event, so that when it records a | ||
new key combination the key event will update in accordance to the new value. You can also | ||
observe the event and perform actions on both key-down and key-up. | ||
|
||
```swift | ||
let event = KeyEvent(name: "SomeEvent") | ||
let recorder = KeyRecorder(keyEvent: event) | ||
|
||
event.observe(.keyDown) { | ||
print("DOWN") | ||
} | ||
event.observe(.keyUp) { | ||
print("UP") | ||
} | ||
``` | ||
For improved type safety, you can create hard-coded key event names that can be referenced | ||
across your app. | ||
|
||
```swift | ||
extension KeyEvent.Name { | ||
static let showPreferences = Self("ShowPreferences") | ||
} | ||
let event = KeyEvent(name: .showPreferences) | ||
``` | ||
|
||
Key events are automatically stored in the `UserDefaults` system, using their names as keys. | ||
You can provide a custom prefix that will be combined with each name to create the keys. | ||
|
||
```swift | ||
extension KeyEvent.Name.Prefix { | ||
public override var sharedPrefix: Self { | ||
Self("SK") | ||
} | ||
} | ||
``` | ||
|
||
The `showPreferences` name from above would become "SKShowPreferences" when used as a | ||
`UserDefaults` key. | ||
|
||
## Topics | ||
|
||
### Creating and Observing Key Events | ||
|
||
- ``KeyEvent`` | ||
- ``KeyEvent/Name-swift.struct`` | ||
- ``KeyEvent/Name-swift.struct/Prefix-swift.class`` | ||
|
||
### Recording Key Events | ||
|
||
- ``KeyRecorder`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// ProxyStorage.swift | ||
// | ||
// Created: 2022. Author: Jordan Baird. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct ProxyStorage: Hashable { | ||
private static var all = Set<Self>() | ||
|
||
private let proxy: EventProxy | ||
|
||
private var identifier: UInt32 { | ||
proxy.identifier.id | ||
} | ||
|
||
private var name: KeyEvent.Name { | ||
proxy.name | ||
} | ||
|
||
private init(_ proxy: EventProxy) { | ||
self.proxy = proxy | ||
} | ||
|
||
static func proxy(with identifier: UInt32) -> EventProxy? { | ||
all.first { $0.identifier == identifier }?.proxy | ||
} | ||
|
||
static func proxy(with name: KeyEvent.Name) -> EventProxy? { | ||
all.first { $0.name == name }?.proxy | ||
} | ||
|
||
static func store(_ proxy: EventProxy) { | ||
all.update(with: .init(proxy)) | ||
} | ||
|
||
static func remove(_ proxy: EventProxy) { | ||
if let storage = all.first(where: { $0 ~= proxy }) { | ||
all.remove(storage) | ||
} | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(identifier) | ||
hasher.combine(name) | ||
} | ||
|
||
static func == (lhs: Self, rhs: Self) -> Bool { | ||
lhs.identifier == rhs.identifier && | ||
lhs.name == rhs.name | ||
} | ||
|
||
static func ~= (lhs: Self, rhs: EventProxy) -> Bool { | ||
lhs.identifier == rhs.identifier.id && | ||
lhs.name == rhs.name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.