2 #import <Carbon/Carbon.h>
4 @implementation ITKeyCombo
7 return [self keyComboWithKeyCode:-1 modifiers:-1];
10 + (id)keyComboWithKeyCode:(int)keyCode modifiers:(int)modifiers {
11 return [[[self alloc] initWithKeyCode:keyCode modifiers:modifiers] autorelease];
14 + (id)keyComboWithPlistRepresentation:(id)plist {
15 return [[[self alloc] initWithPlistRepresentation:plist] autorelease];
18 - (id)initWithKeyCode:(int)keyCode modifiers:(int)modifiers {
19 if ((self = [super init])) {
21 mModifiers = modifiers;
26 - (id)initWithPlistRepresentation:(id)plist {
27 int keyCode, modifiers;
29 keyCode = [[plist objectForKey:@"keyCode"] intValue];
34 modifiers = [[plist objectForKey:@"modifiers"] intValue];
39 return [self initWithKeyCode:keyCode modifiers:modifiers];
42 - (id)plistRepresentation {
43 return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[self keyCode]], @"keyCode", [NSNumber numberWithInt:[self modifiers]], @"modifiers", nil];
46 - (id)copyWithZone:(NSZone *)zone {
50 - (BOOL)isEqual:(ITKeyCombo *)combo {
51 return (([self keyCode] == [combo keyCode]) && ([self modifiers] == [combo modifiers]));
62 - (BOOL)isValidHotKeyCombo {
63 return ((mKeyCode >= 0) && (mModifiers > 0));
66 - (BOOL)isClearCombo {
67 return ((mKeyCode == -1) && (mModifiers == -1));
72 @implementation ITKeyCombo (UserDisplayAdditions)
74 + (NSDictionary *)_keyCodesDictionary {
75 static NSDictionary *keyCodes = nil;
81 path = [[NSBundle bundleForClass:self] pathForResource:@"ITKeyCodes" ofType:@"plist"];
82 contents = [NSString stringWithContentsOfFile:path];
83 keyCodes = [[contents propertyList] retain];
89 + (NSString *)_stringForModifiers:(long)modifiers {
90 static long modToChar[4][2] = {
91 { cmdKey, 0x23180000 },
92 { optionKey, 0x23250000 },
93 { controlKey, 0x23030000 },
94 { shiftKey, 0x21e70000 }
101 for (i = 0;i < 4;i++) {
102 if (modifiers & modToChar[i][0]) {
103 charStr = [NSString stringWithCharacters:(const unichar *)&modToChar[i][1] length:1];
104 str = [str stringByAppendingString:charStr];
111 + (NSString *)_stringForKeyCode:(short)keyCode {
112 NSDictionary *dict = [self _keyCodesDictionary];
113 NSString *key = [NSString stringWithFormat:@"%d", keyCode];
114 NSString *str = [dict objectForKey:key];
117 str = [NSString stringWithFormat:@"%X", keyCode];
123 - (NSString *)description {
124 if ([self isValidHotKeyCombo]) { //This might have to change
125 return [NSString stringWithFormat:@"%@%@", [[self class] _stringForModifiers:[self modifiers]], [[self class] _stringForKeyCode:[self keyCode]]];
128 return NSLocalizedString(@"(None)", @"Hot Keys: Key Combo text for 'empty' combo");