Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Support for re-type the shortcut in MACShortcutView #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Framework/MASHotKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ extern FourCharCode const MASHotKeySignature;
@property(copy) dispatch_block_t action;

+ (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut;
- (BOOL) activate;
- (void) deactivate;

@end
27 changes: 20 additions & 7 deletions Framework/MASHotKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Framework/MASShortcutMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
- (void) unregisterShortcut: (MASShortcut*) shortcut;
- (void) unregisterAllShortcuts;

- (void) setShortcut: (MASShortcut*) shortcut active: (BOOL) active;

@end
6 changes: 6 additions & 0 deletions Framework/MASShortcutMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Framework/MASShortcutView.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "MASShortcutView.h"
#import "MASShortcutValidator.h"
#import "MASShortcutMonitor.h"
#import "MASLocalization.h"

NSString *const MASShortcutBinding = @"shortcutValue";
Expand Down Expand Up @@ -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
Expand Down