-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITKeyBroadcaster.m
58 lines (44 loc) · 1.46 KB
/
ITKeyBroadcaster.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
#import "ITKeyBroadcaster.h"
#import "ITKeyCombo.h"
#import <Carbon/Carbon.h>
NSString *ITKeyBroadcasterKeyEvent = @"ITKeyBroadcasterKeyEvent";
@implementation ITKeyBroadcaster
- (void)_bcastKeyCode:(short)keyCode modifiers:(long)modifiers {
ITKeyCombo *keyCombo = [ITKeyCombo keyComboWithKeyCode:keyCode modifiers:modifiers];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:keyCombo forKey:@"keyCombo"];
[[NSNotificationCenter defaultCenter] postNotificationName:ITKeyBroadcasterKeyEvent object:self userInfo:userInfo];
}
- (void)_bcastEvent:(NSEvent *)event {
short keyCode;
long modifiers;
keyCode = [event keyCode];
modifiers = [event modifierFlags];
modifiers = [[self class] cocoaModifiersAsCarbonModifiers:modifiers];
[self _bcastKeyCode:keyCode modifiers:modifiers];
}
- (void)keyDown:(NSEvent *)event {
[self _bcastEvent:event];
}
- (BOOL)performKeyEquivalent:(NSEvent *)event {
[self _bcastEvent:event];
return YES;
}
+ (long)cocoaModifiersAsCarbonModifiers:(long)cocoaModifiers {
static long cocoaToCarbon[6][2] = {
{ NSCommandKeyMask, cmdKey },
{ NSAlternateKeyMask, optionKey },
{ NSControlKeyMask, controlKey },
{ NSShiftKeyMask, shiftKey },
{ NSFunctionKeyMask, rightControlKey },
//{ NSAlphaShiftKeyMask, alphaLock }, //Ignore this?
};
long carbonModifiers = 0;
int i;
for (i = 0;i < 6;i++) {
if (cocoaModifiers & cocoaToCarbon[i][0]) {
carbonModifiers += cocoaToCarbon[i][1];
}
}
return carbonModifiers;
}
@end