-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSRKeyCodeTransformer.m
231 lines (196 loc) · 6.32 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
228
229
230
231
//
// 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),
@"F17", SRInt(64),
@"F18", SRInt(79),
@"F19", SRInt(80),
SRLoc(@"Space"), SRInt(49),
SRChar(KeyboardDeleteLeftGlyph), SRInt(51),
SRChar(KeyboardDeleteRightGlyph), SRInt(117),
SRChar(KeyboardPadClearGlyph), SRInt(71),
SRChar(KeyboardLeftArrowGlyph), SRInt(123),
SRChar(KeyboardRightArrowGlyph), SRInt(124),
SRChar(KeyboardUpArrowGlyph), SRInt(126),
SRChar(KeyboardDownArrowGlyph), SRInt(125),
SRChar(KeyboardSoutheastArrowGlyph), SRInt(119),
SRChar(KeyboardNorthwestArrowGlyph), SRInt(115),
SRChar(KeyboardEscapeGlyph), SRInt(53),
SRChar(KeyboardPageDownGlyph), SRInt(121),
SRChar(KeyboardPageUpGlyph), SRInt(116),
SRChar(KeyboardReturnR2LGlyph), SRInt(36),
SRChar(KeyboardReturnGlyph), SRInt(76),
SRChar(KeyboardTabRightGlyph), SRInt(48),
SRChar(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];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(regenerateStringToKeyCodeMapping) name:(NSString*)kTISNotifySelectedKeyboardInputSourceChanged object:nil];
}
//----------------------------------------------------------
// allowsReverseTransformation
//----------------------------------------------------------
+ (BOOL) allowsReverseTransformation
{
return YES;
}
//----------------------------------------------------------
// transformedValueClass
//----------------------------------------------------------
+ (Class) transformedValueClass;
{
return [NSString class];
}
//----------------------------------------------------------
// init
//----------------------------------------------------------
- (id)init
{
if((self = [super init]))
{
}
return self;
}
//----------------------------------------------------------
// dealloc
//----------------------------------------------------------
- (void)dealloc
{
[super dealloc];
}
//----------------------------------------------------------
// transformedValue:
//----------------------------------------------------------
- (id) transformedValue:(id)value
{
if ( ![value isKindOfClass:[NSNumber class]] )
return nil;
// Can be -1 when empty
NSInteger 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 )];
OSStatus err;
TISInputSourceRef tisSource = TISCopyCurrentKeyboardInputSource();
if(!tisSource) return nil;
CFDataRef layoutData;
UInt32 keysDown = 0;
layoutData = (CFDataRef)TISGetInputSourceProperty(tisSource, kTISPropertyUnicodeKeyLayoutData);
if(!layoutData) return nil;
const UCKeyboardLayout *keyLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
UniCharCount length = 4, realLength;
UniChar chars[4];
err = UCKeyTranslate( keyLayout,
keyCode,
kUCKeyActionDisplay,
0,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&keysDown,
length,
&realLength,
chars);
if ( err != noErr ) return nil;
NSString *keyString = [[NSString stringWithCharacters:chars length:1] uppercaseString];
return ( isPadKey ? [NSString stringWithFormat: SRLoc(@"Pad %@"), keyString] : keyString );
}
//----------------------------------------------------------
// 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...
NSUInteger i;
for ( i = 0U; i < 128U; i++ )
{
NSNumber *keyCode = [NSNumber numberWithUnsignedInteger:i];
NSString *string = [transformer transformedValue:keyCode];
if ( ( string ) && ( [string length] ) )
{
[stringToKeyCodeDict setObject:keyCode forKey:string];
}
}
}
@end