forked from Kentzo/ShortcutRecorder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSRKeyCodeTransformer.m
341 lines (297 loc) · 11.2 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// SRKeyCodeTransformer.h
// ShortcutRecorder
//
// Copyright 2006-2012 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
// Ilya Kulakov
// Silvio Rizzi
#import "SRKeyCodeTransformer.h"
#import "SRCommon.h"
FOUNDATION_STATIC_INLINE NSString* _SRUnicharToString(unichar aChar)
{
return [NSString stringWithFormat: @"%C", aChar];
}
@implementation SRKeyCodeTransformer
- (instancetype)initWithASCIICapableKeyboardInputSource:(BOOL)aUsesASCII plainStrings:(BOOL)aUsesPlainStrings
{
self = [super init];
if (self)
{
_usesASCIICapableKeyboardInputSource = aUsesASCII;
_usesPlainStrings = aUsesPlainStrings;
}
return self;
}
- (instancetype)init
{
return [self initWithASCIICapableKeyboardInputSource:NO plainStrings:NO];
}
- (void)dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark Methods
+ (instancetype)sharedTransformer
{
static dispatch_once_t OnceToken;
static SRKeyCodeTransformer *Transformer = nil;
dispatch_once(&OnceToken, ^{
Transformer = [[self alloc] initWithASCIICapableKeyboardInputSource:NO
plainStrings:NO];
});
return Transformer;
}
+ (instancetype)sharedASCIITransformer
{
static dispatch_once_t OnceToken;
static SRKeyCodeTransformer *Transformer = nil;
dispatch_once(&OnceToken, ^{
Transformer = [[self alloc] initWithASCIICapableKeyboardInputSource:YES
plainStrings:NO];
});
return Transformer;
}
+ (instancetype)sharedPlainTransformer
{
static dispatch_once_t OnceToken;
static SRKeyCodeTransformer *Transformer = nil;
dispatch_once(&OnceToken, ^{
Transformer = [[self alloc] initWithASCIICapableKeyboardInputSource:NO
plainStrings:YES];
});
return Transformer;
}
+ (SRKeyCodeTransformer *)sharedPlainASCIITransformer
{
static dispatch_once_t OnceToken;
static SRKeyCodeTransformer *Transformer = nil;
dispatch_once(&OnceToken, ^{
Transformer = [[self alloc] initWithASCIICapableKeyboardInputSource:YES
plainStrings:YES];
});
return Transformer;
}
+ (NSDictionary *)specialKeyCodesToUnicodeCharactersMapping
{
static dispatch_once_t OnceToken;
static NSDictionary *Mapping = nil;
dispatch_once(&OnceToken, ^{
Mapping = @{
@(kVK_F1): _SRUnicharToString(NSF1FunctionKey),
@(kVK_F2): _SRUnicharToString(NSF2FunctionKey),
@(kVK_F3): _SRUnicharToString(NSF3FunctionKey),
@(kVK_F4): _SRUnicharToString(NSF4FunctionKey),
@(kVK_F5): _SRUnicharToString(NSF5FunctionKey),
@(kVK_F6): _SRUnicharToString(NSF6FunctionKey),
@(kVK_F7): _SRUnicharToString(NSF7FunctionKey),
@(kVK_F8): _SRUnicharToString(NSF8FunctionKey),
@(kVK_F9): _SRUnicharToString(NSF9FunctionKey),
@(kVK_F10): _SRUnicharToString(NSF10FunctionKey),
@(kVK_F11): _SRUnicharToString(NSF11FunctionKey),
@(kVK_F12): _SRUnicharToString(NSF12FunctionKey),
@(kVK_F13): _SRUnicharToString(NSF13FunctionKey),
@(kVK_F14): _SRUnicharToString(NSF14FunctionKey),
@(kVK_F15): _SRUnicharToString(NSF15FunctionKey),
@(kVK_F16): _SRUnicharToString(NSF16FunctionKey),
@(kVK_F17): _SRUnicharToString(NSF17FunctionKey),
@(kVK_F18): _SRUnicharToString(NSF18FunctionKey),
@(kVK_F19): _SRUnicharToString(NSF19FunctionKey),
@(kVK_F20): _SRUnicharToString(NSF20FunctionKey),
@(kVK_Space): _SRUnicharToString(SRKeyCodeGlyphSpace),
@(kVK_Delete): _SRUnicharToString(SRKeyCodeGlyphDeleteLeft),
@(kVK_ForwardDelete): _SRUnicharToString(SRKeyCodeGlyphDeleteRight),
@(kVK_ANSI_KeypadClear): _SRUnicharToString(SRKeyCodeGlyphPadClear),
@(kVK_LeftArrow): _SRUnicharToString(SRKeyCodeGlyphLeftArrow),
@(kVK_RightArrow): _SRUnicharToString(SRKeyCodeGlyphRightArrow),
@(kVK_UpArrow): _SRUnicharToString(SRKeyCodeGlyphUpArrow),
@(kVK_DownArrow): _SRUnicharToString(SRKeyCodeGlyphDownArrow),
@(kVK_End): _SRUnicharToString(SRKeyCodeGlyphSoutheastArrow),
@(kVK_Home): _SRUnicharToString(SRKeyCodeGlyphNorthwestArrow),
@(kVK_Escape): _SRUnicharToString(SRKeyCodeGlyphEscape),
@(kVK_PageDown): _SRUnicharToString(SRKeyCodeGlyphPageDown),
@(kVK_PageUp): _SRUnicharToString(SRKeyCodeGlyphPageUp),
@(kVK_Return): _SRUnicharToString(SRKeyCodeGlyphReturnR2L),
@(kVK_ANSI_KeypadEnter): _SRUnicharToString(SRKeyCodeGlyphReturn),
@(kVK_Tab): _SRUnicharToString(SRKeyCodeGlyphRight),
@(kVK_Help): _SRUnicharToString(SRKeyCodeGlyphHelp)
};
});
return Mapping;
}
+ (NSDictionary *)specialKeyCodesToPlainStringsMapping
{
static dispatch_once_t OnceToken;
static NSDictionary *Mapping = nil;
dispatch_once(&OnceToken, ^{
Mapping = @{
@(kVK_F1): @"F1",
@(kVK_F2): @"F2",
@(kVK_F3): @"F3",
@(kVK_F4): @"F4",
@(kVK_F5): @"F5",
@(kVK_F6): @"F6",
@(kVK_F7): @"F7",
@(kVK_F8): @"F8",
@(kVK_F9): @"F9",
@(kVK_F10): @"F10",
@(kVK_F11): @"F11",
@(kVK_F12): @"F12",
@(kVK_F13): @"F13",
@(kVK_F14): @"F14",
@(kVK_F15): @"F15",
@(kVK_F16): @"F16",
@(kVK_F17): @"F17",
@(kVK_F18): @"F18",
@(kVK_F19): @"F19",
@(kVK_F20): @"F20",
@(kVK_Space): NSLocalizedStringFromTableInBundle(@"Space",
@"ShortcutRecorder",
[NSBundle bundleWithIdentifier:SRBundleIdentifier],
nil),
@(kVK_Delete): _SRUnicharToString(SRKeyCodeGlyphDeleteLeft),
@(kVK_ForwardDelete): _SRUnicharToString(SRKeyCodeGlyphDeleteRight),
@(kVK_ANSI_KeypadClear): _SRUnicharToString(SRKeyCodeGlyphPadClear),
@(kVK_LeftArrow): _SRUnicharToString(SRKeyCodeGlyphLeftArrow),
@(kVK_RightArrow): _SRUnicharToString(SRKeyCodeGlyphRightArrow),
@(kVK_UpArrow): _SRUnicharToString(SRKeyCodeGlyphUpArrow),
@(kVK_DownArrow): _SRUnicharToString(SRKeyCodeGlyphDownArrow),
@(kVK_End): _SRUnicharToString(SRKeyCodeGlyphSoutheastArrow),
@(kVK_Home): _SRUnicharToString(SRKeyCodeGlyphNorthwestArrow),
@(kVK_Escape): _SRUnicharToString(SRKeyCodeGlyphEscape),
@(kVK_PageDown): _SRUnicharToString(SRKeyCodeGlyphPageDown),
@(kVK_PageUp): _SRUnicharToString(SRKeyCodeGlyphPageUp),
@(kVK_Return): _SRUnicharToString(SRKeyCodeGlyphReturnR2L),
@(kVK_ANSI_KeypadEnter): _SRUnicharToString(SRKeyCodeGlyphReturn),
@(kVK_Tab): _SRUnicharToString(SRKeyCodeGlyphRight),
@(kVK_Help): _SRUnicharToString(SRKeyCodeGlyphHelp)
};
});
return Mapping;
}
- (BOOL)isKeyCodeSpecial:(unsigned short)aKeyCode
{
switch (aKeyCode)
{
case kVK_F1:
case kVK_F2:
case kVK_F3:
case kVK_F4:
case kVK_F5:
case kVK_F6:
case kVK_F7:
case kVK_F8:
case kVK_F9:
case kVK_F10:
case kVK_F11:
case kVK_F12:
case kVK_F13:
case kVK_F14:
case kVK_F15:
case kVK_F16:
case kVK_F17:
case kVK_F18:
case kVK_F19:
case kVK_F20:
case kVK_Space:
case kVK_Delete:
case kVK_ForwardDelete:
case kVK_ANSI_KeypadClear:
case kVK_LeftArrow:
case kVK_RightArrow:
case kVK_UpArrow:
case kVK_DownArrow:
case kVK_End:
case kVK_Home:
case kVK_Escape:
case kVK_PageDown:
case kVK_PageUp:
case kVK_Return:
case kVK_ANSI_KeypadEnter:
case kVK_Tab:
case kVK_Help:
return YES;
default:
return NO;
}
}
#pragma mark NSValueTransformer
+ (BOOL)allowsReverseTransformation
{
return NO;
}
+ (Class)transformedValueClass;
{
return [NSString class];
}
- (NSString *)transformedValue:(NSNumber *)aValue
{
return [self transformedValue:aValue withModifierFlags:nil];
}
- (NSString *)transformedValue:(NSNumber *)aValue withModifierFlags:(NSNumber *)aModifierFlags
{
if (![aValue isKindOfClass:[NSNumber class]])
return @"";
unsigned short keyCode = [aValue unsignedShortValue];
// Some key codes cannot be translated directly.
NSString *unmappedString = nil;
if (self.usesPlainStrings)
unmappedString = [[self class] specialKeyCodesToPlainStringsMapping][@(keyCode)];
else
unmappedString = [[self class] specialKeyCodesToUnicodeCharactersMapping][@(keyCode)];
if (unmappedString)
return unmappedString;
CFDataRef layoutData = NULL;
if (self.usesASCIICapableKeyboardInputSource)
{
TISInputSourceRef tisSource = TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
if (!tisSource)
return @"";
layoutData = (CFDataRef)TISGetInputSourceProperty(tisSource, kTISPropertyUnicodeKeyLayoutData);
CFRelease(tisSource);
}
else
{
TISInputSourceRef tisSource = TISCopyCurrentKeyboardInputSource();
if (!tisSource)
return @"";
layoutData = (CFDataRef)TISGetInputSourceProperty(tisSource, kTISPropertyUnicodeKeyLayoutData);
CFRelease(tisSource);
// For non-unicode layouts such as Chinese, Japanese, and Korean, get the ASCII capable layout
if (!layoutData)
{
tisSource = TISCopyCurrentASCIICapableKeyboardInputSource();
if (!tisSource)
return @"";
layoutData = (CFDataRef)TISGetInputSourceProperty(tisSource, kTISPropertyUnicodeKeyLayoutData);
CFRelease(tisSource);
}
}
if (!layoutData)
return @"";
const UCKeyboardLayout *keyLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
static const UniCharCount MaxLength = 255;
UniCharCount actualLength = 0;
UniChar chars[MaxLength] = {0};
UInt32 deadKeyState = 0;
OSStatus err = UCKeyTranslate(keyLayout,
keyCode,
kUCKeyActionDisplay,
SRCocoaToCarbonFlags([aModifierFlags unsignedIntegerValue]) >> 8,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&deadKeyState,
sizeof(chars) / sizeof(UniChar),
&actualLength,
chars);
if (err != noErr)
return @"";
return [NSString stringWithCharacters:chars length:actualLength];
}
@end