diff options
| author | Tomáš Znamenáček | 2015-01-08 12:00:53 +0100 |
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-08 12:00:53 +0100 |
| commit | 9b919cba51e4cd11b0c4424930d6c18a1baec73c (patch) | |
| tree | a8107774609d5f4263f7b79749d93e6c6ff2642d /Framework/MASHotKey.m | |
| parent | a3a459b4e4e47bf18dccd5dc7f315389346e3d6c (diff) | |
| parent | ea69d5939511f61a7082ba1e8ff46d247862a3fa (diff) | |
| download | MASShortcut-9b919cba51e4cd11b0c4424930d6c18a1baec73c.tar.bz2 | |
Merge pull request #53 from zoul/2.0-candidate
Thank you very much!
Diffstat (limited to 'Framework/MASHotKey.m')
| -rw-r--r-- | Framework/MASHotKey.m | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Framework/MASHotKey.m b/Framework/MASHotKey.m new file mode 100644 index 0000000..7886440 --- /dev/null +++ b/Framework/MASHotKey.m @@ -0,0 +1,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 |
