1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#import "MASShortcutMonitor.h"
#import "MASHotKey.h"
@interface MASShortcutMonitor ()
@property(assign) EventHandlerRef eventHandlerRef;
@property(strong) NSMutableDictionary *hotKeys;
@end
static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*);
@implementation MASShortcutMonitor
#pragma mark Initialization
- (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);
if (status != noErr) {
return nil;
}
return self;
}
- (void) dealloc
{
if (_eventHandlerRef) {
RemoveEventHandler(_eventHandlerRef);
_eventHandlerRef = NULL;
}
}
+ (instancetype) sharedMonitor
{
static dispatch_once_t once;
static MASShortcutMonitor *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark Registration
- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action
{
MASHotKey *hotKey = [MASHotKey registeredHotKeyWithShortcut:shortcut];
if (hotKey) {
[hotKey setAction:action];
[_hotKeys setObject:hotKey forKey:shortcut];
return YES;
} else {
return NO;
}
}
- (void) unregisterShortcut: (MASShortcut*) shortcut
{
if (shortcut) {
[_hotKeys removeObjectForKey:shortcut];
}
}
- (void) unregisterAllShortcuts
{
[_hotKeys removeAllObjects];
}
- (BOOL) isShortcutRegistered: (MASShortcut*) shortcut
{
return !![_hotKeys objectForKey:shortcut];
}
#pragma mark Event Handling
- (void) handleEvent: (EventRef) event
{
if (GetEventClass(event) != kEventClassKeyboard) {
return;
}
EventHotKeyID hotKeyID;
OSStatus status = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (status != noErr || hotKeyID.signature != MASHotKeySignature) {
return;
}
[_hotKeys enumerateKeysAndObjectsUsingBlock:^(MASShortcut *shortcut, MASHotKey *hotKey, BOOL *stop) {
if (hotKeyID.id == [hotKey carbonID]) {
if ([hotKey action]) {
dispatch_async(dispatch_get_main_queue(), [hotKey action]);
}
*stop = YES;
}
}];
}
@end
static OSStatus MASCarbonEventCallback(EventHandlerCallRef _, EventRef event, void *context)
{
MASShortcutMonitor *dispatcher = (__bridge id)context;
[dispatcher handleEvent:event];
return noErr;
}
|