DAMN YOU PROJECT BUILDER FOR IMPORTING ONLY FOUNDATION!!!
[ITKit.git] / ITKeyBroadcaster.m
1 //
2 //  ITKeyBroadcaster.m
3 //
4 //  Created by Quentin Carnicelli on Sun Aug 03 2003.
5 //  Copyright (c) 2003 iThink Software. All rights reserved.
6 //
7
8 #import "ITKeyBroadcaster.h"
9 #import "ITKeyCombo.h"
10 #import <Carbon/Carbon.h>
11
12 NSString* ITKeyBroadcasterKeyEvent = @"ITKeyBroadcasterKeyEvent";
13
14 @implementation ITKeyBroadcaster
15
16 - (void)_bcastKeyCode: (short)keyCode modifiers: (long)modifiers
17 {
18         ITKeyCombo* keyCombo = [ITKeyCombo keyComboWithKeyCode: keyCode modifiers: modifiers];
19         NSDictionary* userInfo = [NSDictionary dictionaryWithObject: keyCombo forKey:@"keyCombo"];
20
21         [[NSNotificationCenter defaultCenter]
22                 postNotificationName: ITKeyBroadcasterKeyEvent
23                 object: self
24                 userInfo: userInfo];
25 }
26
27 - (void)_bcastEvent: (NSEvent*)event
28 {
29         short keyCode;
30         long modifiers;
31         
32         keyCode = [event keyCode];
33         modifiers = [event modifierFlags];
34         modifiers = [[self class] cocoaModifiersAsCarbonModifiers: modifiers];
35
36         [self _bcastKeyCode: keyCode modifiers: modifiers];
37 }
38
39 - (void)keyDown: (NSEvent*)event
40 {
41         [self _bcastEvent: event];
42 }
43
44 - (BOOL)performKeyEquivalent: (NSEvent*)event
45 {
46         [self _bcastEvent: event];
47         return YES;
48 }
49
50 + (long)cocoaModifiersAsCarbonModifiers: (long)cocoaModifiers
51 {
52         static long cocoaToCarbon[6][2] =
53         {
54                 { NSCommandKeyMask, cmdKey},
55                 { NSAlternateKeyMask, optionKey},
56                 { NSControlKeyMask, controlKey},
57                 { NSShiftKeyMask, shiftKey},
58                 { NSFunctionKeyMask, rightControlKey},
59                 //{ NSAlphaShiftKeyMask, alphaLock }, //Ignore this?
60         };
61
62         long carbonModifiers = 0;
63         int i;
64         
65         for( i = 0 ; i < 6; i++ )
66                 if( cocoaModifiers & cocoaToCarbon[i][0] )
67                         carbonModifiers += cocoaToCarbon[i][1];
68         
69         return carbonModifiers;
70 }
71
72
73 @end