-
Notifications
You must be signed in to change notification settings - Fork 2
/
SRKeyCodeTransformer.m
227 lines (181 loc) · 7.99 KB
/
SRKeyCodeTransformer.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
//
// SRKeyCodeTransformer.h
// ShortcutRecorder
//
// Copyright 2006-2007 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
#import "SRKeyCodeTransformer.h"
#import <Carbon/Carbon.h>
#import <CoreServices/CoreServices.h>
#import "SRCommon.h"
static NSMutableDictionary *stringToKeyCodeDict = nil;
static NSDictionary *keyCodeToStringDict = nil;
static NSArray *padKeysArray = nil;
@interface SRKeyCodeTransformer (Private)
+ (void)regenerateStringToKeyCodeMapping;
@end
#pragma mark -
@implementation SRKeyCodeTransformer
//----------------------------------------------------------
// initialize
//----------------------------------------------------------
+ (void)initialize;
{
if (self != [SRKeyCodeTransformer class]) return;
// Some keys need a special glyph
keyCodeToStringDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"F1", SRInt(122),
@"F2", SRInt(120),
@"F3", SRInt(99),
@"F4", SRInt(118),
@"F5", SRInt(96),
@"F6", SRInt(97),
@"F7", SRInt(98),
@"F8", SRInt(100),
@"F9", SRInt(101),
@"F10", SRInt(109),
@"F11", SRInt(103),
@"F12", SRInt(111),
@"F13", SRInt(105),
@"F14", SRInt(107),
@"F15", SRInt(113),
@"F16", SRInt(106),
SRLoc(@"Space"), SRInt(49),
SRChar((unichar)KeyboardDeleteLeftGlyph), SRInt(51),
SRChar((unichar)KeyboardDeleteRightGlyph), SRInt(117),
SRChar((unichar)KeyboardPadClearGlyph), SRInt(71),
SRChar((unichar)KeyboardLeftArrowGlyph), SRInt(123),
SRChar((unichar)KeyboardRightArrowGlyph), SRInt(124),
SRChar((unichar)KeyboardUpArrowGlyph), SRInt(126),
SRChar((unichar)KeyboardDownArrowGlyph), SRInt(125),
SRChar((unichar)KeyboardSoutheastArrowGlyph), SRInt(119),
SRChar((unichar)KeyboardNorthwestArrowGlyph), SRInt(115),
SRChar((unichar)KeyboardEscapeGlyph), SRInt(53),
SRChar((unichar)KeyboardPageDownGlyph), SRInt(121),
SRChar((unichar)KeyboardPageUpGlyph), SRInt(116),
SRChar((unichar)KeyboardReturnR2LGlyph), SRInt(36),
SRChar((unichar)KeyboardReturnGlyph), SRInt(76),
SRChar((unichar)KeyboardTabRightGlyph), SRInt(48),
SRChar((unichar)KeyboardHelpGlyph), SRInt(114),
nil];
// We want to identify if the key was pressed on the numpad
padKeysArray = [[NSArray alloc] initWithObjects:
SRInt(65), // ,
SRInt(67), // *
SRInt(69), // +
SRInt(75), // /
SRInt(78), // -
SRInt(81), // =
SRInt(82), // 0
SRInt(83), // 1
SRInt(84), // 2
SRInt(85), // 3
SRInt(86), // 4
SRInt(87), // 5
SRInt(88), // 6
SRInt(89), // 7
SRInt(91), // 8
SRInt(92), // 9
nil];
// generate the string to keycode mapping dict...
stringToKeyCodeDict = [[NSMutableDictionary alloc] init];
[self regenerateStringToKeyCodeMapping];
}
//----------------------------------------------------------
// allowsReverseTransformation
//----------------------------------------------------------
+ (BOOL)allowsReverseTransformation {
return YES;
}
//----------------------------------------------------------
// transformedValueClass
//----------------------------------------------------------
+ (Class)transformedValueClass;
{
return [NSString class];
}
//----------------------------------------------------------
// transformedValue:
//----------------------------------------------------------
- (id)transformedValue:(id)value {
if (![value isKindOfClass:[NSNumber class]]) return nil;
// Can be -1 when empty
signed short keyCode = [value shortValue];
if (keyCode < 0) return nil;
// We have some special gylphs for some special keys...
NSString *unmappedString = [keyCodeToStringDict objectForKey:SRInt(keyCode)];
if (unmappedString != nil) return unmappedString;
BOOL isPadKey = [padKeysArray containsObject:SRInt(keyCode)];
KeyboardLayoutRef currentLayoutRef;
KeyboardLayoutKind currentLayoutKind;
OSStatus err;
err = KLGetCurrentKeyboardLayout(¤tLayoutRef);
if (err != noErr) return nil;
err = KLGetKeyboardLayoutProperty(currentLayoutRef, kKLKind, (const void **)¤tLayoutKind);
if (err != noErr) return nil;
UInt32 keysDown = 0;
if (currentLayoutKind == kKLKCHRKind) {
Handle kchrHandle;
err = KLGetKeyboardLayoutProperty(currentLayoutRef, kKLKCHRData, (const void **)&kchrHandle);
if (err != noErr) return nil;
UInt32 charCode = KeyTranslate(kchrHandle, keyCode, &keysDown);
if (keysDown != 0) charCode = KeyTranslate(kchrHandle, keyCode, &keysDown);
char theChar = (charCode & 0x00FF);
NSString *keyString = [[[[NSString alloc] initWithData:[NSData dataWithBytes:&theChar length:1] encoding:NSMacOSRomanStringEncoding] autorelease] uppercaseString];
return (isPadKey ? [NSString stringWithFormat:SRLoc(@"Pad %@"), keyString] : keyString);
} else { // kKLuchrKind, kKLKCHRuchrKind
// UCKeyboardLayout *keyboardLayout = NULL;
// err = KLGetKeyboardLayoutProperty(currentLayoutRef, kKLuchrData, (const void **)&keyboardLayout);
if (err != noErr) return nil;
// UniCharCount length = 4, realLength;
UniChar chars[4];
// err = UCKeyTranslate(keyboardLayout,
// keyCode,
// kUCKeyActionDisplay,
// 0,
// LMGetKbdType(),
// kUCKeyTranslateNoDeadKeysBit,
// &keysDown,
// length,
// &realLength,
// chars);
NSString *keyString = [[NSString stringWithCharacters:chars length:1] uppercaseString];
return (isPadKey ? [NSString stringWithFormat:SRLoc(@"Pad %@"), keyString] : keyString);
}
return nil;
}
//----------------------------------------------------------
// reverseTransformedValue:
//----------------------------------------------------------
- (id)reverseTransformedValue:(id)value {
if (![value isKindOfClass:[NSString class]]) return nil;
// try and retrieve a mapped keycode from the reverse mapping dict...
return [stringToKeyCodeDict objectForKey:value];
}
@end
#pragma mark -
@implementation SRKeyCodeTransformer (Private)
//----------------------------------------------------------
// regenerateStringToKeyCodeMapping:
//----------------------------------------------------------
+ (void)regenerateStringToKeyCodeMapping;
{
SRKeyCodeTransformer *transformer = [[[self alloc] init] autorelease];
[stringToKeyCodeDict removeAllObjects];
// loop over every keycode (0 - 127) finding its current string mapping...
unsigned i;
for (i = 0U; i < 128U; i++) {
NSNumber *keyCode = [NSNumber numberWithUnsignedInt:i];
NSString *string = [transformer transformedValue:keyCode];
if ( (string) && ([string length]) ) {
[stringToKeyCodeDict setObject:keyCode forKey:string];
}
}
}
@end