33
44@interface MASShortcutBinder ()
55@property (strong ) NSMutableDictionary *actions;
6+ @property (strong ) NSMutableDictionary *actionsUp;
67@property (strong ) NSMutableDictionary *shortcuts;
78@end
89
@@ -14,6 +15,7 @@ - (id) init
1415{
1516 self = [super init ];
1617 [self setActions: [NSMutableDictionary dictionary ]];
18+ [self setActionsUp: [NSMutableDictionary dictionaryWithCapacity: 0 ]];
1719 [self setShortcuts: [NSMutableDictionary dictionary ]];
1820 [self setShortcutMonitor: [MASShortcutMonitor sharedMonitor ]];
1921 [self setBindingOptions: @{NSValueTransformerNameBindingOption : NSKeyedUnarchiveFromDataTransformerName }];
@@ -40,23 +42,33 @@ + (instancetype) sharedBinder
4042#pragma mark Registration
4143
4244- (void ) bindShortcutWithDefaultsKey : (NSString *) defaultsKeyName toAction : (dispatch_block_t ) action
45+ {
46+ [self bindShortcutWithDefaultsKey: defaultsKeyName toAction: action onKeyUp: nil ];
47+ }
48+
49+ - (void ) bindShortcutWithDefaultsKey : (NSString *) defaultsKeyName toAction : (dispatch_block_t ) action onKeyUp : (dispatch_block_t ) actionUp
4350{
4451 NSAssert ([defaultsKeyName rangeOfString: @" ." ].location == NSNotFound ,
45- @" Illegal character in binding name (“.”), please see http://git.io/x5YS." );
52+ @" Illegal character in binding name (“.”), please see http://git.io/x5YS." );
4653 NSAssert ([defaultsKeyName rangeOfString: @" " ].location == NSNotFound ,
47- @" Illegal character in binding name (“ ”), please see http://git.io/x5YS." );
54+ @" Illegal character in binding name (“ ”), please see http://git.io/x5YS." );
4855 [_actions setObject: [action copy ] forKey: defaultsKeyName];
56+ if (actionUp)
57+ [_actionsUp setObject: [actionUp copy ] forKey: defaultsKeyName];
58+ else
59+ [_actionsUp removeObjectForKey: defaultsKeyName];
4960 [self bind: defaultsKeyName
50- toObject: [NSUserDefaultsController sharedUserDefaultsController ]
51- withKeyPath: [@" values." stringByAppendingString: defaultsKeyName]
52- options: _bindingOptions];
61+ toObject: [NSUserDefaultsController sharedUserDefaultsController ]
62+ withKeyPath: [@" values." stringByAppendingString: defaultsKeyName]
63+ options: _bindingOptions];
5364}
5465
5566- (void ) breakBindingWithDefaultsKey : (NSString *) defaultsKeyName
5667{
5768 [_shortcutMonitor unregisterShortcut: [_shortcuts objectForKey: defaultsKeyName]];
5869 [_shortcuts removeObjectForKey: defaultsKeyName];
5970 [_actions removeObjectForKey: defaultsKeyName];
71+ [_actionsUp removeObjectForKey: defaultsKeyName];
6072 [self unbind: defaultsKeyName];
6173}
6274
@@ -115,7 +127,7 @@ - (void) setValue: (id) value forUndefinedKey: (NSString*) key
115127
116128 // Bind new shortcut
117129 [_shortcuts setObject: newShortcut forKey: key];
118- [_shortcutMonitor registerShortcut: newShortcut withAction: [_actions objectForKey: key]];
130+ [_shortcutMonitor registerShortcut: newShortcut withAction: [_actions objectForKey: key] onKeyUp: [_actionsUp objectForKey: key] ];
119131}
120132
121133@end
0 commit comments