1 #import "ITKeyBroadcaster.h"
3 #import <Carbon/Carbon.h>
5 NSString *ITKeyBroadcasterKeyEvent = @"ITKeyBroadcasterKeyEvent";
7 @implementation ITKeyBroadcaster
9 - (void)_bcastKeyCode:(short)keyCode modifiers:(long)modifiers {
10 ITKeyCombo *keyCombo = [ITKeyCombo keyComboWithKeyCode:keyCode modifiers:modifiers];
11 NSDictionary *userInfo = [NSDictionary dictionaryWithObject:keyCombo forKey:@"keyCombo"];
13 [[NSNotificationCenter defaultCenter] postNotificationName:ITKeyBroadcasterKeyEvent object:self userInfo:userInfo];
16 - (void)_bcastEvent:(NSEvent *)event {
20 keyCode = [event keyCode];
21 modifiers = [event modifierFlags];
22 modifiers = [[self class] cocoaModifiersAsCarbonModifiers:modifiers];
24 [self _bcastKeyCode:keyCode modifiers:modifiers];
27 - (void)keyDown:(NSEvent *)event {
28 [self _bcastEvent:event];
31 - (BOOL)performKeyEquivalent:(NSEvent *)event {
32 [self _bcastEvent:event];
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?
46 long carbonModifiers = 0;
49 for (i = 0;i < 6;i++) {
50 if (cocoaModifiers & cocoaToCarbon[i][0]) {
51 carbonModifiers += cocoaToCarbon[i][1];
55 return carbonModifiers;