public
Description: KeyCastr, an open-source keystroke visualizer
Homepage: http://stephendeken.net/software/keycastr/
Clone URL: git://github.com/sdeken/keycastr.git
keycastr / KCKeystrokeTransformer.m
100644 210 lines (183 sloc) 5.904 kb
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
// Copyright (c) 2009 Stephen Deken
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name KeyCastr nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
#import "KCKeystrokeTransformer.h"
#import "KCKeystroke.h"
 
@interface KCKeystrokeTransformer (Private)
 
-(NSDictionary*) _shiftedSpecialKeys;
-(NSDictionary*) _specialKeys;
 
@end
 
 
@implementation KCKeystrokeTransformer
 
static NSString* kCommandKeyString = nil;
static NSString* kAltKeyString = nil;
static NSString* kControlKeyString = nil;
static NSString* kShiftKeyString = nil;
 
#define UTF8(x) [NSString stringWithUTF8String:x]
#define NSNum(x) [NSNumber numberWithInt:x]
 
+(void) load
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
kControlKeyString = [[NSString stringWithUTF8String:"\xe2\x8c\x83\x01"] retain];
kAltKeyString = [[NSString stringWithUTF8String:"\xe2\x8c\xa5\x01"] retain];
kCommandKeyString = [[NSString stringWithUTF8String:"\xe2\x8c\x98\x01"] retain];
kShiftKeyString = [[NSString stringWithUTF8String:"\xe2\x87\xa7\x01"] retain];
[pool release];
}
 
+(BOOL) allowsReverseTransformation
{
return NO;
}
 
+(Class) transformedValueClass
{
return [NSString class];
}
 
+(KCKeystrokeTransformer*) sharedTransformer
{
static KCKeystrokeTransformer* xformer = nil;
if (xformer == nil)
{
xformer = [[KCKeystrokeTransformer alloc] init];
}
return xformer;
}
 
-(NSDictionary*) _shiftedSpecialKeys
{
static NSDictionary *d = nil;
if (d == nil)
{
d = [[NSDictionary alloc] initWithObjectsAndKeys:
UTF8("\xe2\x87\xa4"), NSNum(48), // tab
nil];
}
return d;
}
 
-(NSDictionary*) _specialKeys
{
static NSDictionary *d = nil;
if (d == nil)
{
d = [[NSDictionary alloc] initWithObjectsAndKeys:
UTF8("\xe2\x87\xa1"), NSNum(126), // up
UTF8("\xe2\x87\xa3"), NSNum(125), // down
UTF8("\xe2\x87\xa2"), NSNum(124), // right
UTF8("\xe2\x87\xa0"), NSNum(123), // left
UTF8("\xe2\x87\xa5"), NSNum(48), // tab
UTF8("\xe2\x8e\x8b"), NSNum(53), // escape
UTF8("\xe2\x8e\x8b"), NSNum(71), // escape
UTF8("\xe2\x8c\xab"), NSNum(51), // delete
UTF8("\xe2\x8c\xa6"), NSNum(117), // forward delete
UTF8("?\xe2\x83\x9d"), NSNum(114), // help
UTF8("\xe2\x86\x96"), NSNum(115), // home
UTF8("\xe2\x86\x98"), NSNum(119), // end
UTF8("\xe2\x87\x9e"), NSNum(116), // pgup
UTF8("\xe2\x87\x9f"), NSNum(121), // pgdn
UTF8("\xe2\x86\xa9"), NSNum(36), // return
UTF8("\xe2\x86\xa9"), NSNum(76), // numpad enter
UTF8("F1"), NSNum(122), // F1
UTF8("F2"), NSNum(120), // F2
UTF8("F3"), NSNum(99), // F3
UTF8("F4"), NSNum(118), // F4
UTF8("F5"), NSNum(96), // F5
UTF8("F6"), NSNum(97), // F6
UTF8("F7"), NSNum(98), // F7
UTF8("F8"), NSNum(100), // F8
UTF8("F9"), NSNum(101), // F9
UTF8("F10"), NSNum(109), // F10
UTF8("F11"), NSNum(103), // F11
UTF8("F12"), NSNum(111), // F12
UTF8("F13"), NSNum(105), // F13
UTF8("F14"), NSNum(107), // F14
UTF8("F15"), NSNum(113), // F15
UTF8("F16"), NSNum(106), // F16
UTF8("\xe2\x90\xa3\xe2\x80\x8b"), NSNum(49), // space
nil];
}
return d;
}
 
-(id) transformedValue:(id)value
{
KCKeystroke* v = (KCKeystroke*)value;
NSMutableString* s = [NSMutableString string];
 
uint32_t _modifiers = [v modifiers];
uint16_t _keyCode = [v keyCode];
uint16_t _charCode = [v charCode];
 
BOOL isShifted = NO;
BOOL needsShiftGlyph = NO;
BOOL isCommand = NO;
 
if (_modifiers & NSControlKeyMask)
{
isCommand = YES;
[s appendString:kControlKeyString];
}
if (_modifiers & NSAlternateKeyMask)
{
isCommand = YES;
[s appendString:kAltKeyString];
}
if (_modifiers & NSShiftKeyMask)
{
isShifted = YES;
if (isCommand)
[s appendString:kShiftKeyString];
else
needsShiftGlyph = YES;
}
if (_modifiers & NSCommandKeyMask)
{
if (needsShiftGlyph)
{
[s appendString:kShiftKeyString];
needsShiftGlyph = NO;
}
isCommand = YES;
[s appendString:kCommandKeyString];
}
 
if (isShifted)
{
id tmp = [[self _shiftedSpecialKeys] objectForKey:NSNum(_keyCode)];
if (tmp != nil)
{
[s appendString:tmp];
return s;
}
}
 
id tmp = [[self _specialKeys] objectForKey:NSNum(_keyCode)];
if (tmp != nil)
{
if (needsShiftGlyph)
[s appendString:[NSString stringWithUTF8String:"\xe2\x87\xa7\x01"]];
[s appendString:tmp];
 
return s;
}
 
[s appendString:[NSString stringWithCharacters:&_charCode length:1]];
 
// If this is a command string, put it in uppercase.
if (isCommand)
{
NSMutableString *t = [[s uppercaseString] mutableCopy];
s = t;
}
 
return s;
}
 
@end