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)keyComboWithPlistRepresentation: (id)plist
26 return [[[self alloc] initWithPlistRepresentation: plist] autorelease];
29 - (id)initWithKeyCode: (int)keyCode modifiers: (int)modifiers
36 mModifiers = modifiers;
42 - (id)initWithPlistRepresentation: (id)plist
44 int keyCode, modifiers;
46 keyCode = [[plist objectForKey: @"keyCode"] intValue];
47 if( keyCode <= 0 ) keyCode = -1;
49 modifiers = [[plist objectForKey: @"modifiers"] intValue];
50 if( modifiers <= 0 ) modifiers = -1;
52 return [self initWithKeyCode: keyCode modifiers: modifiers];
55 - (id)plistRepresentation
57 return [NSDictionary dictionaryWithObjectsAndKeys:
58 [NSNumber numberWithInt: [self keyCode]], @"keyCode",
59 [NSNumber numberWithInt: [self modifiers]], @"modifiers",
63 - (id)copyWithZone:(NSZone*)zone;
68 - (BOOL)isEqual: (ITKeyCombo*)combo
70 return [self keyCode] == [combo keyCode] &&
71 [self modifiers] == [combo modifiers];
86 - (BOOL)isValidHotKeyCombo
88 return mKeyCode >= 0 && mModifiers > 0;
93 return mKeyCode == -1 && mModifiers == -1;
100 @implementation ITKeyCombo (UserDisplayAdditions)
102 + (NSDictionary*)_keyCodesDictionary
104 static NSDictionary* keyCodes = nil;
106 if( keyCodes == nil )
111 path = [[NSBundle bundleForClass: self] pathForResource: @"ITKeyCodes" ofType: @"plist"];
112 contents = [NSString stringWithContentsOfFile: path];
113 keyCodes = [[contents propertyList] retain];
119 + (NSString*)_stringForModifiers: (long)modifiers
121 static long modToChar[4][2] =
123 { cmdKey, 0x23180000 },
124 { optionKey, 0x23250000 },
125 { controlKey, 0x005E0000 },
126 { shiftKey, 0x21e70000 }
133 str = [NSString string];
135 for( i = 0; i < 4; i++ )
137 if( modifiers & modToChar[i][0] )
139 charStr = [NSString stringWithCharacters: (const unichar*)&modToChar[i][1] length: 1];
140 str = [str stringByAppendingString: charStr];
150 + (NSString*)_stringForKeyCode: (short)keyCode
156 dict = [self _keyCodesDictionary];
157 key = [NSString stringWithFormat: @"%d", keyCode];
158 str = [dict objectForKey: key];
161 str = [NSString stringWithFormat: @"%X", keyCode];
166 - (NSString*)description
170 if( [self isValidHotKeyCombo] ) //This might have to change
172 desc = [NSString stringWithFormat: @"%@%@",
173 [[self class] _stringForModifiers: [self modifiers]],
174 [[self class] _stringForKeyCode: [self keyCode]]];
177 desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );