-
Notifications
You must be signed in to change notification settings - Fork 3
/
DGGHotKey.m
132 lines (98 loc) · 4.67 KB
/
DGGHotKey.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//*******************************************************************************
// Copyright (c) 2012 Danny Greg
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Created by Danny Greg on 29/6/2012
//*******************************************************************************
#import "DGGHotKey.h"
#import "DGGKeyCombo.h"
#import <Carbon/Carbon.h>
@interface DGGHotKey ()
@property (nonatomic) UInt32 hotKeyID;
@property (nonatomic, copy) void(^handlerBlock)();
@property (nonatomic) EventHotKeyRef carbonHotKey;
static OSStatus DGGHotKeyPressedHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon);
@end
@implementation DGGHotKey
BOOL DGGHotKeyHandlerInstalled = NO;
UInt32 DGGLatestHotKeyID = 0;
NSMutableDictionary *DGGHotKeyBlockMap = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfour-char-constants"
FourCharCode DGGHotKeyCarbonID = 'DGGH';
#pragma clang diagnostic pop
+ (void)initialize
{
DGGHotKeyBlockMap = [[NSMutableDictionary alloc] init];
EventTypeSpec keyDownSpec = {kEventClassKeyboard, kEventHotKeyPressed};
OSStatus result = InstallEventHandler(GetEventDispatcherTarget(), DGGHotKeyPressedHandler, 1, &keyDownSpec, NULL, NULL);
DGGHotKeyHandlerInstalled = (result == noErr);
}
+ (id)registerHotKeyForKeyCombo:(DGGKeyCombo *)combo withError:(NSError **)error usingBlock:(void(^)())handlerBlock
{
void (^assignErrorWithCodeMessage)(NSInteger, NSString *) = ^ (NSInteger code, NSString *errorMessage) {
if (error == NULL)
return;
*error = [NSError errorWithDomain:@"com.dannygreg.dgghotkey" code:code userInfo:@{ NSLocalizedDescriptionKey : errorMessage }];
};
if (!DGGHotKeyHandlerInstalled) {
assignErrorWithCodeMessage(DGGHotKeyErrorNoHandler, NSLocalizedString(@"Could not initialise global hot key handler", nil));
return nil;
}
UInt32 carbonModifierFlags = 0;
if (combo.modifierMask & NSCommandKeyMask) carbonModifierFlags |= cmdKey;
if (combo.modifierMask & NSAlternateKeyMask) carbonModifierFlags |= optionKey;
if (combo.modifierMask & NSControlKeyMask) carbonModifierFlags |= controlKey;
if (combo.modifierMask & NSShiftKeyMask) carbonModifierFlags |= shiftKey;
EventHotKeyID hotKeyID = {DGGHotKeyCarbonID, ++DGGLatestHotKeyID};
EventHotKeyRef carbonHotKey = NULL;
OSStatus result = RegisterEventHotKey((UInt32)combo.keyCode, carbonModifierFlags, hotKeyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &carbonHotKey);
if (result != noErr) {
assignErrorWithCodeMessage(result, NSLocalizedString(@"Could not register global hotkey", nil));
return nil;
}
DGGHotKey *hotKey = [[DGGHotKey alloc] init];
hotKey.handlerBlock = handlerBlock;
hotKey.hotKeyID = hotKeyID.id;
hotKey.carbonHotKey = carbonHotKey;
[DGGHotKeyBlockMap setObject:hotKey forKey:[NSNumber numberWithUnsignedInteger:hotKeyID.id]];
return hotKey;
}
+ (BOOL)unregisterHotKeyWithIdentifier:(id)identifier
{
if (identifier == nil)
return YES;
return (UnregisterEventHotKey([identifier carbonHotKey]) == noErr);
}
#pragma mark - Carbon Callbacks
static OSStatus DGGHotKeyPressedHandler(EventHandlerCallRef inHandlerRef, EventRef event, void* refCon)
{
if (GetEventClass(event) != kEventClassKeyboard)
return noErr;
EventHotKeyID hotKeyID;
OSStatus result = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (result != noErr)
return result;
if (hotKeyID.signature != DGGHotKeyCarbonID)
return noErr;
DGGHotKey *hotKey = [DGGHotKeyBlockMap objectForKey:[NSNumber numberWithUnsignedInteger:hotKeyID.id]];
if (hotKey == nil)
return noErr;
if (hotKey.handlerBlock != nil) //It would be dumb… but let's be safe
hotKey.handlerBlock();
return noErr;
}
@end