aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/MASHotKey.m
blob: c5ab74445042b3a4c93c5a2961c7ec95e951e72f (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(), 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