forked from cocoabits/MASShortcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMASHotKey.m
46 lines (34 loc) · 990 Bytes
/
MASHotKey.m
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
#import <AppKit/AppKit.h>
#import <Carbon/Carbon.h>
#import "./MASHotKey.h"
FourCharCode const MASHotKeySignature = 'MASS';
@interface MASHotKey ()
@property(assign) EventHotKeyRef hotKeyRef;
@property(assign) UInt32 carbonID;
@end
@implementation MASHotKey
- (instancetype) initWithShortcut: (MASShortcut*) shortcut
{
self = [super init];
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) {
return nil;
}
return self;
}
+ (instancetype) registeredHotKeyWithShortcut: (MASShortcut*) shortcut
{
return [[self alloc] initWithShortcut:shortcut];
}
- (void) dealloc
{
if (_hotKeyRef) {
UnregisterEventHotKey(_hotKeyRef);
_hotKeyRef = NULL;
}
}
@end