Fixed a crash when Core Image was available.
[ITKit.git] / ITKeyBroadcaster.m
1 #import "ITKeyBroadcaster.h"
2 #import "ITKeyCombo.h"
3 #import <Carbon/Carbon.h>
4
5 NSString *ITKeyBroadcasterKeyEvent = @"ITKeyBroadcasterKeyEvent";
6
7 @implementation ITKeyBroadcaster
8
9 - (void)_bcastKeyCode:(short)keyCode modifiers:(long)modifiers {
10         ITKeyCombo *keyCombo = [ITKeyCombo keyComboWithKeyCode:keyCode modifiers:modifiers];
11         NSDictionary *userInfo = [NSDictionary dictionaryWithObject:keyCombo forKey:@"keyCombo"];
12         
13         [[NSNotificationCenter defaultCenter] postNotificationName:ITKeyBroadcasterKeyEvent object:self userInfo:userInfo];
14 }
15
16 - (void)_bcastEvent:(NSEvent *)event {
17         short keyCode;
18         long modifiers;
19         
20         keyCode = [event keyCode];
21         modifiers = [event modifierFlags];
22         modifiers = [[self class] cocoaModifiersAsCarbonModifiers:modifiers];
23         
24         [self _bcastKeyCode:keyCode modifiers:modifiers];
25 }
26
27 - (void)keyDown:(NSEvent *)event {
28         [self _bcastEvent:event];
29 }
30
31 - (BOOL)performKeyEquivalent:(NSEvent *)event {
32         [self _bcastEvent:event];
33         return YES;
34 }
35
36 + (long)cocoaModifiersAsCarbonModifiers:(long)cocoaModifiers {
37         static long cocoaToCarbon[6][2] = {
38                 { NSCommandKeyMask, cmdKey },
39                 { NSAlternateKeyMask, optionKey },
40                 { NSControlKeyMask, controlKey },
41                 { NSShiftKeyMask, shiftKey },
42                 { NSFunctionKeyMask, rightControlKey },
43                 //{ NSAlphaShiftKeyMask, alphaLock }, //Ignore this?
44         };
45         
46         long carbonModifiers = 0;
47         int i;
48         
49         for (i = 0;i < 6;i++) {
50                 if (cocoaModifiers & cocoaToCarbon[i][0]) {
51                         carbonModifiers += cocoaToCarbon[i][1];
52                 }
53         }
54         
55         return carbonModifiers;
56 }
57
58 @end