diff --git a/Framework/MASHotKey.h b/Framework/MASHotKey.h index 1d267e4..62a71d6 100644 --- a/Framework/MASHotKey.h +++ b/Framework/MASHotKey.h @@ -8,5 +8,7 @@ extern FourCharCode const MASHotKeySignature; @property(copy) dispatch_block_t action; + (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut; +- (BOOL) activate; +- (void) deactivate; @end diff --git a/Framework/MASHotKey.m b/Framework/MASHotKey.m index c5ab744..f9aa5aa 100644 --- a/Framework/MASHotKey.m +++ b/Framework/MASHotKey.m @@ -5,6 +5,8 @@ @interface MASHotKey () @property(assign) EventHotKeyRef hotKeyRef; @property(assign) UInt32 carbonID; +@property(assign) UInt32 carbonKeyCode; +@property(assign) UInt32 carbonFlags; @end @implementation MASHotKey @@ -16,14 +18,11 @@ - (instancetype) initWithShortcut: (MASShortcut*) shortcut static UInt32 CarbonHotKeyID = 0; _carbonID = ++CarbonHotKeyID; - EventHotKeyID hotKeyID = { .signature = MASHotKeySignature, .id = _carbonID }; - - OSStatus status = RegisterEventHotKey([shortcut carbonKeyCode], [shortcut carbonFlags], - hotKeyID, GetEventDispatcherTarget(), 0, &_hotKeyRef); - - if (status != noErr) { + _carbonKeyCode = [shortcut carbonKeyCode]; + _carbonFlags = [shortcut carbonFlags]; + + if (![self activate]) return nil; - } return self; } @@ -35,6 +34,20 @@ + (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut - (void) dealloc { + [self deactivate]; +} + +- (BOOL) activate { + if (_hotKeyRef) + return YES; + + EventHotKeyID hotKeyID = { .signature = MASHotKeySignature, .id = _carbonID }; + OSStatus status = RegisterEventHotKey(_carbonKeyCode, _carbonFlags, hotKeyID, + GetEventDispatcherTarget(), 0, &_hotKeyRef); + return status == noErr; +} + +- (void) deactivate { if (_hotKeyRef) { UnregisterEventHotKey(_hotKeyRef); _hotKeyRef = NULL; diff --git a/Framework/MASShortcutMonitor.h b/Framework/MASShortcutMonitor.h index dc3d458..302b33d 100644 --- a/Framework/MASShortcutMonitor.h +++ b/Framework/MASShortcutMonitor.h @@ -24,4 +24,6 @@ - (void) unregisterShortcut: (MASShortcut*) shortcut; - (void) unregisterAllShortcuts; +- (void) setShortcut: (MASShortcut*) shortcut active: (BOOL) active; + @end diff --git a/Framework/MASShortcutMonitor.m b/Framework/MASShortcutMonitor.m index fce8022..24907b6 100644 --- a/Framework/MASShortcutMonitor.m +++ b/Framework/MASShortcutMonitor.m @@ -74,6 +74,12 @@ - (BOOL) isShortcutRegistered: (MASShortcut*) shortcut return !![_hotKeys objectForKey:shortcut]; } +- (void) setShortcut: (MASShortcut*) shortcut active: (BOOL) active +{ + MASHotKey *hotKey = [_hotKeys objectForKey:shortcut]; + active ? [hotKey activate] : [hotKey deactivate]; +} + #pragma mark Event Handling - (void) handleEvent: (EventRef) event diff --git a/Framework/MASShortcutView.m b/Framework/MASShortcutView.m index 9b27a10..8759a29 100644 --- a/Framework/MASShortcutView.m +++ b/Framework/MASShortcutView.m @@ -1,5 +1,6 @@ #import "MASShortcutView.h" #import "MASShortcutValidator.h" +#import "MASShortcutMonitor.h" #import "MASLocalization.h" NSString *const MASShortcutBinding = @"shortcutValue"; @@ -136,6 +137,7 @@ - (void)setRecording:(BOOL)flag [self activateEventMonitoring:_recording]; [self activateResignObserver:_recording]; [self setNeedsDisplay:YES]; + [[MASShortcutMonitor sharedMonitor] setShortcut:[self shortcutValue] active:!flag]; // Give VoiceOver users feedback on the result. Requires at least 10.9 to run. // We’re silencing the “tautological compare” warning here so that if someone