-
Notifications
You must be signed in to change notification settings - Fork 4
/
NJRHotKeyManager.m
300 lines (235 loc) · 7.44 KB
/
NJRHotKeyManager.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// NJRHotKeyManager.m
// Pester
//
// Created by Nicholas Riley on Tue Apr 01 2003.
// Copyright (c) 2003 Nicholas Riley. All rights reserved.
//
// based on HotKeyCenter, by Quentin Carnicelli
// renamed, reorganized, cleaned up, pre-10.2 support removed
#import "NJRHotKeyManager.h"
#import "NJRHotKey.h"
#import <Carbon/Carbon.h>
#if !__LP64__
const OSType kHotKeyManagerSignature = 'NHKM';
#endif
@interface _NJRHotKeyShortcut : NSObject {
@public
BOOL isRegistered;
EventHotKeyRef hotKeyRef;
NJRHotKey *hotKey;
id target;
SEL action;
}
@end
@implementation _NJRHotKeyShortcut
- (void)dealloc;
{
[hotKey release];
[target release];
[super dealloc];
}
- (NSString *)description;
{
return [NSString stringWithFormat: @"<%@: %@ %sregistered [%@ %@]>", [self className], [hotKey keyGlyphs],
isRegistered ? "" : "not ", target, NSStringFromSelector(action)];
}
@end
pascal OSErr keyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *refCon);
@interface NJRHotKeyManager (Private)
- (OSStatus)_handleHotKeyEvent:(EventRef)inEvent;
- (BOOL)_registerHotKeyIfNeeded:(_NJRHotKeyShortcut *)shortcut;
- (void)_unregisterHotKeyIfNeeded:(_NJRHotKeyShortcut *)shortcut;
- (void)_hotKeyDown:(_NJRHotKeyShortcut *)hotKey;
- (void)_hotKeyUp:(_NJRHotKeyShortcut *)hotKey;
- (void)_hotKeyDownWithRef:(EventHotKeyRef)ref;
- (void)_hotKeyUpWithRef:(EventHotKeyRef)ref;
- (_NJRHotKeyShortcut *)_findShortcutWithRef:(EventHotKeyRef)ref;
@end
@implementation NJRHotKeyManager
+ (NJRHotKeyManager *)sharedManager;
{
static NJRHotKeyManager *manager = nil;
if (manager == nil) {
manager = [[self alloc] init];
EventTypeSpec eventSpec[2] = {
{ kEventClassKeyboard, kEventHotKeyPressed },
{ kEventClassKeyboard, kEventHotKeyReleased }
};
InstallEventHandler(GetEventDispatcherTarget(),
NewEventHandlerUPP((EventHandlerProcPtr) keyEventHandler),
2, eventSpec, nil, nil);
}
return manager;
}
- (id)init;
{
if ( (self = [super init]) != nil) {
shortcutsEnabled = YES;
shortcuts = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc;
{
[shortcuts release];
[super dealloc];
}
#pragma mark -
- (BOOL)addShortcutWithIdentifier:(NSString *)identifier hotKey:(NJRHotKey *)hotKey target:(id)target action:(SEL)action;
{
NSParameterAssert(identifier != nil);
NSParameterAssert(hotKey != nil);
NSParameterAssert(target != nil);
NSParameterAssert(action != nil);
if ([shortcuts objectForKey: identifier] != nil)
[self removeShortcutWithIdentifier: identifier];
if (hotKey == [NJRHotKey noHotKey])
return YES;
_NJRHotKeyShortcut *newShortcut = [[_NJRHotKeyShortcut alloc] init];
newShortcut->isRegistered = NO;
newShortcut->hotKeyRef = nil;
newShortcut->hotKey = [hotKey retain];
newShortcut->target = [target retain];
newShortcut->action = action;
[shortcuts setObject: newShortcut forKey: identifier];
[newShortcut release];
return [self _registerHotKeyIfNeeded: newShortcut];
}
- (void)removeShortcutWithIdentifier:(NSString *)identifier;
{
_NJRHotKeyShortcut *hotKey = [shortcuts objectForKey: identifier];
if (hotKey == nil) return;
[self _unregisterHotKeyIfNeeded: hotKey];
[shortcuts removeObjectForKey: identifier];
}
- (NSArray *)shortcutIdentifiers;
{
return [shortcuts allKeys];
}
- (NJRHotKey *)hotKeyForShortcutWithIdentifier:(NSString *)identifier;
{
_NJRHotKeyShortcut *hotKey = [shortcuts objectForKey: identifier];
return (hotKey == nil ? nil : [[hotKey->hotKey retain] autorelease]);
}
- (BOOL)hotKeyInUseWithCode:(NSInteger)keyCode modifierFlags:(NSUInteger)modifierFlags;
{
NSEnumerator *enumerator = [shortcuts objectEnumerator];
_NJRHotKeyShortcut *hotKey;
while ( (hotKey = [enumerator nextObject]) != nil) {
if ([hotKey->hotKey keyCode] == keyCode && [hotKey->hotKey modifierFlags] == modifierFlags)
return YES;
}
return NO;
}
- (void)setShortcutsEnabled:(BOOL)enabled;
{
if (enabled == shortcutsEnabled)
return;
shortcutsEnabled = enabled;
NSEnumerator *enumerator = [shortcuts objectEnumerator];
_NJRHotKeyShortcut *hotKey;
while ( (hotKey = [enumerator nextObject]) != nil) {
if (enabled)
[self _registerHotKeyIfNeeded: hotKey];
else
[self _unregisterHotKeyIfNeeded: hotKey];
}
}
- (BOOL)shortcutsEnabled;
{
return shortcutsEnabled;
}
#pragma mark -
- (OSStatus)_handleHotKeyEvent:(EventRef)inEvent;
{
OSStatus err;
EventHotKeyID hotKeyID;
_NJRHotKeyShortcut *shortcut;
NSAssert(GetEventClass(inEvent) == kEventClassKeyboard, @"Unhandled event class");
if ( (err = GetEventParameter(inEvent, kEventParamDirectObject, typeEventHotKeyID, nil,
sizeof(EventHotKeyID), nil, &hotKeyID)) != noErr)
return err;
#if !__LP64__
NSAssert(hotKeyID.signature == kHotKeyManagerSignature, @"Unknown hot key");
#endif
#if __LP64__
shortcut = (_NJRHotKeyShortcut *)((NSUInteger)hotKeyID.signature << 32 | (NSUInteger)hotKeyID.id);
#else
shortcut = (_NJRHotKeyShortcut *)hotKeyID.id;
#endif
NSAssert(shortcut != nil, @"Got bad hot key");
switch (GetEventKind(inEvent)) {
case kEventHotKeyPressed:
[self _hotKeyDown: shortcut];
break;
case kEventHotKeyReleased:
[self _hotKeyUp: shortcut];
break;
default:
break;
}
return noErr;
}
#pragma mark -
- (BOOL)_registerHotKeyIfNeeded:(_NJRHotKeyShortcut *)shortcut;
{
NJRHotKey *hotKey;
NSParameterAssert(shortcut != nil);
hotKey = shortcut->hotKey;
if (shortcutsEnabled && !(shortcut->isRegistered)) {
EventHotKeyID keyID;
#if __LP64__
keyID.signature = (NSUInteger)shortcut >> 32;
keyID.id = (UInt32)shortcut;
#else
keyID.signature = kHotKeyManagerSignature;
keyID.id = (UInt32)shortcut;
#endif
if (RegisterEventHotKey([hotKey keyCode], [hotKey modifiers], keyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &shortcut->hotKeyRef) != noErr)
return NO;
shortcut->isRegistered = YES;
}
return YES;
}
- (void)_unregisterHotKeyIfNeeded:(_NJRHotKeyShortcut *)shortcut;
{
NSParameterAssert(shortcut != nil);
if (shortcut->isRegistered && shortcut->hotKeyRef != nil) {
UnregisterEventHotKey(shortcut->hotKeyRef);
shortcut->isRegistered = NO;
}
}
- (void)_hotKeyDown:(_NJRHotKeyShortcut *)hotKey;
{
id target = hotKey->target;
SEL action = hotKey->action;
[target performSelector: action withObject: self];
}
- (void)_hotKeyUp:(_NJRHotKeyShortcut *)hotKey;
{
}
- (void)_hotKeyDownWithRef:(EventHotKeyRef)ref;
{
_NJRHotKeyShortcut *hotKey = [self _findShortcutWithRef: ref];
if (hotKey != nil)
[self _hotKeyDown: hotKey];
}
- (void)_hotKeyUpWithRef:(EventHotKeyRef)ref;
{
}
- (_NJRHotKeyShortcut *)_findShortcutWithRef:(EventHotKeyRef)ref;
{
NSEnumerator *enumerator = [shortcuts objectEnumerator];
_NJRHotKeyShortcut *hotKey;
while ( (hotKey = [enumerator nextObject]) != nil) {
if (hotKey->hotKeyRef == ref)
return hotKey;
}
return nil;
}
@end
pascal OSErr keyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *refCon)
{
return [[NJRHotKeyManager sharedManager] _handleHotKeyEvent: inEvent];
}