4 // Created by Quentin D. Carnicelli on Tue Jun 18 2002.
5 // Copyright (c) 2001 Subband inc.. All rights reserved.
10 #import <AppKit/NSEvent.h>
11 #import <Carbon/Carbon.h>
13 @interface KeyCombo (Private)
14 + (NSString*)_stringForModifiers:(long)modifiers;
15 + (NSString*)_stringForKeyCode:(short)keyCode;
19 @implementation KeyCombo
23 return [[[self alloc] init] autorelease];
28 return [self keyComboWithKeyCode:-1 andModifiers:-1];
31 + (id)keyComboWithKeyCode: (short)keycode andModifiers: (long)modifiers
33 return [[[self alloc] initWithKeyCode:keycode andModifiers:modifiers] autorelease];
36 - (id)initWithKeyCode: (short)keycode andModifiers: (long)modifiers
38 if ( (self = [super init]) )
41 mModifiers = modifiers;
48 return [self initWithKeyCode: -1 andModifiers: -1];
51 - (id)copyWithZone:(NSZone *)zone;
56 - (id)initWithCoder:(NSCoder *)aDecoder
62 [aDecoder decodeValueOfObjCType: @encode(short) at: &mKeyCode];
63 [aDecoder decodeValueOfObjCType: @encode(long) at: &mModifiers];
69 - (void)encodeWithCoder:(NSCoder *)aCoder
71 [aCoder encodeValueOfObjCType:@encode(short) at:&mKeyCode];
72 [aCoder encodeValueOfObjCType:@encode(long) at:&mModifiers];
75 - (BOOL)isEqual:(KeyCombo *)object
77 return ([object isKindOfClass:[KeyCombo class]]) &&
78 ([object keyCode] == [self keyCode]) &&
79 ([object modifiers] == [self modifiers]);
82 - (NSString *)description
84 return [self userDisplayRep];
99 return ((mKeyCode >= 0) && (mModifiers >= 0));
102 - (NSString *)userDisplayRep
104 if ([self isValid] == NO)
110 return [NSString stringWithFormat: @"%@%@",
111 [KeyCombo _stringForModifiers: mModifiers],
112 [KeyCombo _stringForKeyCode: mKeyCode]];
116 + (NSString *)_stringForModifiers: (long)modifiers
118 static long modToChar[4][2] =
120 { cmdKey, 0x23180000 },
121 { optionKey, 0x23250000 },
122 { controlKey, 0x005E0000 },
123 { shiftKey, 0x21e70000 }
126 NSString *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];
142 + (NSString *)_stringForKeyCode:(short)keyCode
148 dict = [self keyCodesDictionary];
149 key = [NSString stringWithFormat: @"%d", keyCode];
150 str = [dict objectForKey: key];
153 str = [NSString stringWithFormat: @"%X", keyCode];
158 + (NSDictionary *)keyCodesDictionary
160 static NSDictionary *keyCodes = nil;
167 path = [[NSBundle bundleForClass: [KeyCombo class]] pathForResource: @"KeyCodes" ofType: @"plist"];
169 contents = [NSString stringWithContentsOfFile: path];
170 keyCodes = [[contents propertyList] retain];
178 @implementation NSUserDefaults (KeyComboAdditions)
180 - (void)setKeyCombo:(KeyCombo *)combo forKey:(NSString *)key
185 data = [NSArchiver archivedDataWithRootObject:combo];
191 [self setObject:data forKey:key];
194 - (KeyCombo *)keyComboForKey:(NSString *)key
196 NSData *data = [self objectForKey:key];
200 combo = [[NSUnarchiver unarchiveObjectWithData:data] retain];
205 combo = [[KeyCombo alloc] init];