blob: 7886440d3f1058c22bf5f967ad6111b1a308e187 (
plain)
| 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
 | #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(), kEventHotKeyExclusive, &_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
 |