diff options
| author | Vadim Shpakovski | 2012-11-10 12:31:58 +0300 |
|---|---|---|
| committer | Vadim Shpakovski | 2012-11-10 12:34:05 +0300 |
| commit | bc56cdc907541cb633e8787424e0112ed856a5e9 (patch) | |
| tree | bb5383d51a7e33e86083f2d1476a249b95f59881 | |
| parent | 3d4235f879e6f31b9bbcfb6b0fa2178de14d88aa (diff) | |
| download | MASShortcut-bc56cdc907541cb633e8787424e0112ed856a5e9.tar.bz2 | |
Now the component properly handles your trying to set another handler for an existing shortcut.
Only the first assignable handler will be working, the second and other handlers all will be ignored. Also, previous workaround with calling performSelector:withObject:afterDelay: is removed, it seems to be working with an apprpriate use of objc_setAssociatedObject.
| -rw-r--r-- | MASShortcut+Monitoring.m | 19 | ||||
| -rw-r--r-- | MASShortcut+UserDefaults.m | 2 |
2 files changed, 14 insertions, 7 deletions
diff --git a/MASShortcut+Monitoring.m b/MASShortcut+Monitoring.m index 1460f6b..bd0282b 100644 --- a/MASShortcut+Monitoring.m +++ b/MASShortcut+Monitoring.m @@ -2,8 +2,8 @@ NSMutableDictionary *MASRegisteredHotKeys(); BOOL InstallCommonEventHandler(); +BOOL InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey); void UninstallEventHandler(); -void InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey); #pragma mark - @@ -24,8 +24,12 @@ void InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, + (id)addGlobalHotkeyMonitorWithShortcut:(MASShortcut *)shortcut handler:(void (^)())handler { - NSString *monitor = [NSString stringWithFormat:@"%p: %@", shortcut, shortcut.description]; + NSString *monitor = [NSString stringWithFormat:@"%@", shortcut.description]; + if ([MASRegisteredHotKeys() objectForKey:monitor]) return nil; + MASShortcutHotKey *hotKey = [[MASShortcutHotKey alloc] initWithShortcut:shortcut handler:handler]; + if (hotKey == nil) return nil; + [MASRegisteredHotKeys() setObject:hotKey forKey:monitor]; return monitor; } @@ -59,7 +63,9 @@ void InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, if (self) { _shortcut = shortcut; _handler = [handler copy]; - InstallHotkeyWithShortcut(shortcut, &_carbonHotKeyID, &_carbonHotKey); + + if (!InstallHotkeyWithShortcut(shortcut, &_carbonHotKeyID, &_carbonHotKey)) + self = nil; } return self; } @@ -95,19 +101,20 @@ NSMutableDictionary *MASRegisteredHotKeys() FourCharCode const kMASShortcutSignature = 'MASS'; -void InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey) +BOOL InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey) { - if ((shortcut == nil) || !InstallCommonEventHandler()) return; + if ((shortcut == nil) || !InstallCommonEventHandler()) return NO; static UInt32 sCarbonHotKeyID = 0; EventHotKeyID hotKeyID = { .signature = kMASShortcutSignature, .id = ++ sCarbonHotKeyID }; EventHotKeyRef carbonHotKey = NULL; if (RegisterEventHotKey(shortcut.carbonKeyCode, shortcut.carbonFlags, hotKeyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &carbonHotKey) != noErr) { - carbonHotKey = NULL; + return NO; } if (outCarbonHotKeyID) *outCarbonHotKeyID = hotKeyID.id; if (outCarbonHotKey) *outCarbonHotKey = carbonHotKey; + return YES; } static OSStatus CarbonCallback(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) diff --git a/MASShortcut+UserDefaults.m b/MASShortcut+UserDefaults.m index 534830a..3bdbab9 100644 --- a/MASShortcut+UserDefaults.m +++ b/MASShortcut+UserDefaults.m @@ -73,7 +73,7 @@ - (void)userDefaultsDidChange:(NSNotification *)note { [MASShortcut removeGlobalHotkeyMonitor:self.monitor]; - [self performSelector:@selector(installHotKeyFromUserDefaults) withObject:nil afterDelay:0.0]; + [self installHotKeyFromUserDefaults]; } - (void)installHotKeyFromUserDefaults |
