4 // Created by Quentin Carnicelli on Sat Aug 02 2003.
5 // Copyright (c) 2003 iThink Software. All rights reserved.
10 #import <Carbon/Carbon.h>
12 @implementation ITKeyCombo
16 return [self keyComboWithKeyCode: -1 modifiers: -1];
19 + (id)keyComboWithKeyCode: (int)keyCode modifiers: (int)modifiers
21 return [[[self alloc] initWithKeyCode: keyCode modifiers: modifiers] autorelease];
24 - (id)initWithKeyCode: (int)keyCode modifiers: (int)modifiers
31 mModifiers = modifiers;
37 - (id)initWithPlistRepresentation: (id)plist
39 int keyCode, modifiers;
41 keyCode = [[plist objectForKey: @"keyCode"] intValue];
42 if( keyCode <= 0 ) keyCode = -1;
44 modifiers = [[plist objectForKey: @"modifiers"] intValue];
45 if( modifiers <= 0 ) modifiers = -1;
47 return [self initWithKeyCode: keyCode modifiers: modifiers];
50 - (id)plistRepresentation
52 return [NSDictionary dictionaryWithObjectsAndKeys:
53 [NSNumber numberWithInt: [self keyCode]], @"keyCode",
54 [NSNumber numberWithInt: [self modifiers]], @"modifiers",
58 - (id)copyWithZone:(NSZone*)zone;
63 - (BOOL)isEqual: (ITKeyCombo*)combo
65 return [self keyCode] == [combo keyCode] &&
66 [self modifiers] == [combo modifiers];
81 - (BOOL)isValidHotKeyCombo
83 return mKeyCode >= 0 && mModifiers > 0;
88 return mKeyCode == -1 && mModifiers == -1;
95 @implementation ITKeyCombo (UserDisplayAdditions)
97 + (NSDictionary*)_keyCodesDictionary
99 static NSDictionary* keyCodes = nil;
101 if( keyCodes == nil )
106 path = [[NSBundle bundleForClass: self] pathForResource: @"ITKeyCodes" ofType: @"plist"];
107 contents = [NSString stringWithContentsOfFile: path];
108 keyCodes = [[contents propertyList] retain];
114 + (NSString*)_stringForModifiers: (long)modifiers
116 static long modToChar[4][2] =
118 { cmdKey, 0x23180000 },
119 { optionKey, 0x23250000 },
120 { controlKey, 0x005E0000 },
121 { shiftKey, 0x21e70000 }
128 str = [NSString string];
130 for( i = 0; i < 4; i++ )
132 if( modifiers & modToChar[i][0] )
134 charStr = [NSString stringWithCharacters: (const unichar*)&modToChar[i][1] length: 1];
135 str = [str stringByAppendingString: charStr];
145 + (NSString*)_stringForKeyCode: (short)keyCode
151 dict = [self _keyCodesDictionary];
152 key = [NSString stringWithFormat: @"%d", keyCode];
153 str = [dict objectForKey: key];
156 str = [NSString stringWithFormat: @"%X", keyCode];
161 - (NSString*)description
165 if( [self isValidHotKeyCombo] ) //This might have to change
167 desc = [NSString stringWithFormat: @"%@%@",
168 [[self class] _stringForModifiers: [self modifiers]],
169 [[self class] _stringForKeyCode: [self keyCode]]];
172 desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );