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

Add a new initWithKeyUp initializer and corresponding sharedMonitorWithKeyUp to MASShortCutMonitor #111

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
5 changes: 5 additions & 0 deletions Framework/MASShortcutMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
@interface MASShortcutMonitor : NSObject

- (instancetype) init __unavailable;
- (instancetype) initWithKeyUp;
+ (instancetype) sharedMonitor;
+ (instancetype) sharedMonitorWithKeyUp;

- (OSStatus) installEventHandlersWithKeyup: (BOOL) keyUp;
- (OSStatus) installEventHandlers: (int) itemCount withEventTypeSpecs: (EventTypeSpec *) eventTypeSpecs;

/**
Register a shortcut along with an action.
Expand Down
63 changes: 60 additions & 3 deletions Framework/MASShortcutMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ - (instancetype) init
{
self = [super init];
[self setHotKeys:[NSMutableDictionary dictionary]];
EventTypeSpec hotKeyPressedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed };
OSStatus status = InstallEventHandler(GetEventDispatcherTarget(), MASCarbonEventCallback,
1, &hotKeyPressedSpec, (__bridge void*)self, &_eventHandlerRef);

OSStatus status = [self installEventHandlersWithKeyup:NO];

if (status != noErr) {
return nil;
}
return self;
}

- (instancetype) initWithKeyUp
{
self = [super init];
[self setHotKeys:[NSMutableDictionary dictionary]];

OSStatus status = [self installEventHandlersWithKeyup:YES];

if (status != noErr) {
return nil;
}
Expand All @@ -43,6 +56,50 @@ + (instancetype) sharedMonitor
return sharedInstance;
}

+ (instancetype) sharedMonitorWithKeyUp
{
static dispatch_once_t once;
static MASShortcutMonitor *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] initWithKeyUp];
});
return sharedInstance;
}

- (OSStatus) installEventHandlersWithKeyup: (BOOL) keyUp
{
EventTypeSpec hotKeyPressedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed };


OSStatus status;

if (keyUp) {
EventTypeSpec hotKeyReleasedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyReleased };
struct EventTypeSpec *eventTypeSpecs = malloc(sizeof(struct EventTypeSpec) * 2);
eventTypeSpecs[0] = hotKeyPressedSpec;
eventTypeSpecs[1] = hotKeyReleasedSpec;

status = [self installEventHandlers:2 withEventTypeSpecs:eventTypeSpecs];
} else {
status = [self installEventHandlers:1 withEventTypeSpecs:&hotKeyPressedSpec];
}

return status;
}

- (OSStatus) installEventHandlers: (int) itemCount withEventTypeSpecs: (EventTypeSpec *) eventTypeSpecs
{
OSStatus status = InstallEventHandler(GetEventDispatcherTarget(),
MASCarbonEventCallback,
itemCount,
eventTypeSpecs,
(__bridge void*)self,
&_eventHandlerRef);

return status;
}


#pragma mark Registration

- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action
Expand Down