forked from jessegrosjean/quickcursor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PTKeyCombo.m
245 lines (192 loc) · 5.01 KB
/
PTKeyCombo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// PTKeyCombo.m
// Protein
//
// Created by Quentin Carnicelli on Sat Aug 02 2003.
// Copyright (c) 2003 Quentin D. Carnicelli. All rights reserved.
//
#import "PTKeyCombo.h"
#import "PTKeyCodeTranslator.h"
@implementation PTKeyCombo
+ (id)clearKeyCombo
{
return [self keyComboWithKeyCode: -1 modifiers: -1];
}
+ (id)keyComboWithKeyCode: (NSInteger)keyCode modifiers: (NSUInteger)modifiers
{
return [[[self alloc] initWithKeyCode: keyCode modifiers: modifiers] autorelease];
}
- (id)initWithKeyCode: (NSInteger)keyCode modifiers: (NSUInteger)modifiers
{
self = [super init];
if( self )
{
mKeyCode = keyCode;
mModifiers = modifiers;
}
return self;
}
- (id)initWithPlistRepresentation: (id)plist
{
int keyCode, modifiers;
if( !plist || ![plist count] )
{
keyCode = -1;
modifiers = -1;
}
else
{
keyCode = [[plist objectForKey: @"keyCode"] intValue];
if( keyCode < 0 ) keyCode = -1;
modifiers = [[plist objectForKey: @"modifiers"] intValue];
if( modifiers <= 0 ) modifiers = -1;
}
return [self initWithKeyCode: keyCode modifiers: modifiers];
}
- (id)plistRepresentation
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger: [self keyCode]], @"keyCode",
[NSNumber numberWithInteger: [self modifiers]], @"modifiers",
nil];
}
- (id)copyWithZone:(NSZone*)zone;
{
return [self retain];
}
- (BOOL)isEqual: (PTKeyCombo*)combo
{
return [self keyCode] == [combo keyCode] &&
[self modifiers] == [combo modifiers];
}
#pragma mark -
- (NSInteger)keyCode
{
return mKeyCode;
}
- (NSUInteger)modifiers
{
return mModifiers;
}
- (BOOL)isValidHotKeyCombo
{
return mKeyCode >= 0 && mModifiers > 0;
}
- (BOOL)isClearCombo
{
return mKeyCode == -1 && mModifiers == -1;
}
@end
#pragma mark -
@implementation PTKeyCombo (UserDisplayAdditions)
+ (NSString*)_stringForModifiers: (long)modifiers
{
static unichar modToChar[4][2] =
{
{ cmdKey, kCommandUnicode },
{ optionKey, kOptionUnicode },
{ controlKey, kControlUnicode },
{ shiftKey, kShiftUnicode }
};
NSString* str = [NSString string];
long i;
for( i = 0; i < 4; i++ )
{
if( modifiers & modToChar[i][0] )
str = [str stringByAppendingString: [NSString stringWithCharacters: &modToChar[i][1] length: 1]];
}
return str;
}
+ (NSDictionary*)_keyCodesDictionary
{
static NSDictionary* keyCodes = nil;
if( keyCodes == nil )
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle bundleForClass: self] pathForResource: @"PTKeyCodes" ofType: @"plist"]];
NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
keyCodes = [[contents propertyList] retain];
}
return keyCodes;
}
+ (NSString*)_stringForKeyCode: (short)keyCode legacyKeyCodeMap: (NSDictionary*)dict
{
id key;
NSString* str;
key = [NSString stringWithFormat: @"%d", keyCode];
str = [dict objectForKey: key];
if( !str )
str = [NSString stringWithFormat: @"%X", keyCode];
return str;
}
+ (NSString*)_stringForKeyCode: (short)keyCode newKeyCodeMap: (NSDictionary*)dict
{
NSString* result;
NSString* keyCodeStr;
NSDictionary* unmappedKeys;
NSArray* padKeys;
keyCodeStr = [NSString stringWithFormat: @"%d", keyCode];
//Handled if its not handled by translator
unmappedKeys = [dict objectForKey:@"unmappedKeys"];
result = [unmappedKeys objectForKey: keyCodeStr];
if( result )
return result;
//Translate it
result = [[[PTKeyCodeTranslator currentTranslator] translateKeyCode:keyCode] uppercaseString];
//Handle if its a key-pad key
padKeys = [dict objectForKey:@"padKeys"];
if( [padKeys indexOfObject: keyCodeStr] != NSNotFound )
{
result = [NSString stringWithFormat:@"%@ %@", [dict objectForKey:@"padKeyString"], result];
}
return result;
}
+ (NSString*)_stringForKeyCode: (short)keyCode
{
NSDictionary* dict;
dict = [self _keyCodesDictionary];
if( [[dict objectForKey: @"version"] intValue] <= 0 )
return [self _stringForKeyCode: keyCode legacyKeyCodeMap: dict];
return [self _stringForKeyCode: keyCode newKeyCodeMap: dict];
}
- (NSString*)keyCodeString
{
// special case: the modifiers for the "clear" key are 0x0
if ( [self isClearCombo] ) return @"";
return [[self class] _stringForKeyCode:[self keyCode]];
}
- (NSUInteger)modifierMask
{
// special case: the modifiers for the "clear" key are 0x0
if ( [self isClearCombo] ) return 0;
static NSUInteger modToChar[4][2] =
{
{ cmdKey, NSCommandKeyMask },
{ optionKey, NSAlternateKeyMask },
{ controlKey, NSControlKeyMask },
{ shiftKey, NSShiftKeyMask }
};
NSUInteger i, ret = 0;
for ( i = 0; i < 4; i++ )
{
if ( [self modifiers] & modToChar[i][0] ) {
ret |= modToChar[i][1];
}
}
return ret;
}
- (NSString*)description
{
NSString* desc;
if( [self isValidHotKeyCombo] ) //This might have to change
{
desc = [NSString stringWithFormat: @"%@%@",
[[self class] _stringForModifiers: [self modifiers]],
[[self class] _stringForKeyCode: [self keyCode]]];
}
else
{
desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );
}
return desc;
}
@end