Skip to content

Commit

Permalink
Add code sample for Objective-C.
Browse files Browse the repository at this point in the history
Fixes #123
  • Loading branch information
Kentzo committed Apr 18, 2020
1 parent 631b609 commit bc97470
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The framework comes with:
- `SRShortcutValidator` to check validity of the shortcut against Cocoa key equivalents and global hot keys
- `NSValueTransformer` and `NSFormatter` subclasses for custom alterations

In Swift:

```swift
import ShortcutRecorder

Expand All @@ -55,6 +57,29 @@ recorder.bind(.value, to: defaults, withKeyPath: keyPath, options: options)
recorder.objectValue = Shortcut(keyEquivalent: "⇧⌘A")
```

In Objective-C:

```objective-c
#import <ShortcutRecorder/ShortcutRecorder.h>

NSUserDefaultsController *defaults = NSUserDefaultsController.sharedUserDefaultsController;
NSString *keyPath = @"values.shortcut";
NSDictionary *options = @{NSValueTransformerNameBindingOption: NSKeyedUnarchiveFromDataTransformerName};

SRShortcutAction *beepAction = [SRShortcutAction shortcutActionWithKeyPath:keyPath
ofObject:defaults
actionHandler:^BOOL(SRShortcutAction *anAction) {
NSBeep();
return YES;
}];
[[SRGlobalShortcutMonitor sharedMonitor] addAction:beepAction forKeyEvent:SRKeyEventTypeDown];

SRRecorderControl *recorder = [[SRRecorderControl alloc] init];
[recorder bind:NSValueBinding toObject:defaults withKeyPath:keyPath options:options];

recorder.objectValue = [SRShortcut shortcutWithKeyEquivalent:@"⇧⌘A"];
```
## Integration
The framework supports [module maps](https://clang.llvm.org/docs/Modules.html), explicit linking is not required: simply `import ShortcutRecorder` / `#import <ShortcutRecorder/ShortcutRecorder.h>`
Expand Down

0 comments on commit bc97470

Please sign in to comment.