Allow Command+Click to enter a raw key code.
[enjoyable.git] / Classes / NJKeyInputField.m
1 //
2 // NJKeyInputField.h
3 // Enjoyable
4 //
5 // Copyright 2013 Joe Wreschnig.
6 //
7
8 #import "NJKeyInputField.h"
9
10 #include <Carbon/Carbon.h>
11 // Only used for kVK_... codes.
12
13 enum {
14 kVK_RightCommand = kVK_Command - 1,
15 kVK_MAX = 0xFFFF,
16 };
17
18 const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
19
20 @implementation NJKeyInputField {
21 NSTextField *field;
22 NSImageView *warning;
23 }
24
25 - (id)initWithFrame:(NSRect)frameRect {
26 if ((self = [super initWithFrame:frameRect])) {
27 field = [[NSTextField alloc] initWithFrame:self.bounds];
28 field.alignment = NSCenterTextAlignment;
29 field.editable = NO;
30 field.selectable = NO;
31 field.delegate = self;
32 [self addSubview:field];
33
34 warning = [[NSImageView alloc] init];
35 warning.image = [NSImage imageNamed:@"NSInvalidDataFreestanding"];
36 CGSize imgSize = warning.image.size;
37 CGRect bounds = self.bounds;
38 warning.frame = CGRectMake(bounds.size.width - (imgSize.width + 4),
39 (bounds.size.height - imgSize.height) / 2,
40 imgSize.width, imgSize.height);
41
42 warning.toolTip = NSLocalizedString(@"invalid key code",
43 @"shown when the user types an invalid key code");
44 warning.hidden = YES;
45 [self addSubview:warning];
46 }
47 return self;
48 }
49
50 - (void)clear {
51 self.keyCode = NJKeyInputFieldEmpty;
52 [self.delegate keyInputFieldDidClear:self];
53 [self resignIfFirstResponder];
54 }
55
56 - (BOOL)hasKeyCode {
57 return self.keyCode != NJKeyInputFieldEmpty;
58 }
59
60 + (NSString *)stringForKeyCode:(CGKeyCode)keyCode {
61 switch (keyCode) {
62 case kVK_F1: return @"F1";
63 case kVK_F2: return @"F2";
64 case kVK_F3: return @"F3";
65 case kVK_F4: return @"F4";
66 case kVK_F5: return @"F5";
67 case kVK_F6: return @"F6";
68 case kVK_F7: return @"F7";
69 case kVK_F8: return @"F8";
70 case kVK_F9: return @"F9";
71 case kVK_F10: return @"F10";
72 case kVK_F11: return @"F11";
73 case kVK_F12: return @"F12";
74 case kVK_F13: return @"F13";
75 case kVK_F14: return @"F14";
76 case kVK_F15: return @"F15";
77 case kVK_F16: return @"F16";
78 case kVK_F17: return @"F17";
79 case kVK_F18: return @"F18";
80 case kVK_F19: return @"F19";
81 case kVK_F20: return @"F20";
82
83 case kVK_Escape: return @"⎋";
84 case kVK_ANSI_Grave: return @"`";
85
86 case kVK_ANSI_1: return @"1";
87 case kVK_ANSI_2: return @"2";
88 case kVK_ANSI_3: return @"3";
89 case kVK_ANSI_4: return @"4";
90 case kVK_ANSI_5: return @"5";
91 case kVK_ANSI_6: return @"6";
92 case kVK_ANSI_7: return @"7";
93 case kVK_ANSI_8: return @"8";
94 case kVK_ANSI_9: return @"9";
95 case kVK_ANSI_0: return @"0";
96 case kVK_ANSI_Minus: return @"-";
97 case kVK_ANSI_Equal: return @"=";
98
99 case kVK_Function: return @"Fn";
100 case kVK_CapsLock: return @"⇪";
101 case kVK_Command: return NSLocalizedString(@"Left ⌘", @"keyboard key");
102 case kVK_RightCommand: return NSLocalizedString(@"Right ⌘", @"keyboard key");
103 case kVK_Option: return NSLocalizedString(@"Left ⌥", @"keyboard key");
104 case kVK_RightOption: return NSLocalizedString(@"Right ⌥", @"keyboard key");
105 case kVK_Control: return NSLocalizedString(@"Left ⌃", @"keyboard key");
106 case kVK_RightControl: return NSLocalizedString(@"Right ⌃", @"keyboard key");
107 case kVK_Shift: return NSLocalizedString(@"Left ⇧", @"keyboard key");
108 case kVK_RightShift: return NSLocalizedString(@"Right ⇧", @"keyboard key");
109
110 case kVK_Home: return @"↖";
111 case kVK_PageUp: return @"⇞";
112 case kVK_End: return @"↘";
113 case kVK_PageDown: return @"⇟";
114
115 case kVK_ForwardDelete: return @"⌦";
116 case kVK_Delete: return @"⌫";
117
118 case kVK_Tab: return @"⇥";
119 case kVK_Return: return @"↩";
120 case kVK_Space: return @"␣";
121
122 case kVK_ANSI_A: return @"A";
123 case kVK_ANSI_B: return @"B";
124 case kVK_ANSI_C: return @"C";
125 case kVK_ANSI_D: return @"D";
126 case kVK_ANSI_E: return @"E";
127 case kVK_ANSI_F: return @"F";
128 case kVK_ANSI_G: return @"G";
129 case kVK_ANSI_H: return @"H";
130 case kVK_ANSI_I: return @"I";
131 case kVK_ANSI_J: return @"J";
132 case kVK_ANSI_K: return @"K";
133 case kVK_ANSI_L: return @"L";
134 case kVK_ANSI_M: return @"M";
135 case kVK_ANSI_N: return @"N";
136 case kVK_ANSI_O: return @"O";
137 case kVK_ANSI_P: return @"P";
138 case kVK_ANSI_Q: return @"Q";
139 case kVK_ANSI_R: return @"R";
140 case kVK_ANSI_S: return @"S";
141 case kVK_ANSI_T: return @"T";
142 case kVK_ANSI_U: return @"U";
143 case kVK_ANSI_V: return @"V";
144 case kVK_ANSI_W: return @"W";
145 case kVK_ANSI_X: return @"X";
146 case kVK_ANSI_Y: return @"Y";
147 case kVK_ANSI_Z: return @"Z";
148 case kVK_ANSI_LeftBracket: return @"[";
149 case kVK_ANSI_RightBracket: return @"]";
150 case kVK_ANSI_Backslash: return @"\\";
151 case kVK_ANSI_Semicolon: return @";";
152 case kVK_ANSI_Quote: return @"'";
153 case kVK_ANSI_Comma: return @",";
154 case kVK_ANSI_Period: return @".";
155 case kVK_ANSI_Slash: return @"/";
156
157 case kVK_ANSI_Keypad0: return NSLocalizedString(@"Key Pad 0", @"numeric pad key");
158 case kVK_ANSI_Keypad1: return NSLocalizedString(@"Key Pad 1", @"numeric pad key");
159 case kVK_ANSI_Keypad2: return NSLocalizedString(@"Key Pad 2", @"numeric pad key");
160 case kVK_ANSI_Keypad3: return NSLocalizedString(@"Key Pad 3", @"numeric pad key");
161 case kVK_ANSI_Keypad4: return NSLocalizedString(@"Key Pad 4", @"numeric pad key");
162 case kVK_ANSI_Keypad5: return NSLocalizedString(@"Key Pad 5", @"numeric pad key");
163 case kVK_ANSI_Keypad6: return NSLocalizedString(@"Key Pad 6", @"numeric pad key");
164 case kVK_ANSI_Keypad7: return NSLocalizedString(@"Key Pad 7", @"numeric pad key");
165 case kVK_ANSI_Keypad8: return NSLocalizedString(@"Key Pad 8", @"numeric pad key");
166 case kVK_ANSI_Keypad9: return NSLocalizedString(@"Key Pad 9", @"numeric pad key");
167 case kVK_ANSI_KeypadClear: return @"⌧";
168 case kVK_ANSI_KeypadEnter: return @"⌤";
169
170 case kVK_ANSI_KeypadEquals:
171 return NSLocalizedString(@"Key Pad =", @"numeric pad key");
172 case kVK_ANSI_KeypadDivide:
173 return NSLocalizedString(@"Key Pad /", @"numeric pad key");
174 case kVK_ANSI_KeypadMultiply:
175 return NSLocalizedString(@"Key Pad *", @"numeric pad key");
176 case kVK_ANSI_KeypadMinus:
177 return NSLocalizedString(@"Key Pad -", @"numeric pad key");
178 case kVK_ANSI_KeypadPlus:
179 return NSLocalizedString(@"Key Pad +", @"numeric pad key");
180 case kVK_ANSI_KeypadDecimal:
181 return NSLocalizedString(@"Key Pad .", @"numeric pad key");
182
183 case kVK_LeftArrow: return @"←";
184 case kVK_RightArrow: return @"→";
185 case kVK_UpArrow: return @"↑";
186 case kVK_DownArrow: return @"↓";
187
188 case kVK_JIS_Yen: return @"¥";
189 case kVK_JIS_Underscore: return @"_";
190 case kVK_JIS_KeypadComma:
191 return NSLocalizedString(@"Key Pad ,", @"numeric pad key");
192 case kVK_JIS_Eisu: return @"英数";
193 case kVK_JIS_Kana: return @"かな";
194
195
196 case kVK_MAX: // NJKeyInputFieldEmpty
197 return @"";
198 default:
199 return [[NSString alloc] initWithFormat:
200 NSLocalizedString(@"key 0x%x", @"unknown key code"),
201 keyCode];
202 }
203 }
204
205 - (BOOL)acceptsFirstResponder {
206 return self.isEnabled;
207 }
208
209 - (BOOL)becomeFirstResponder {
210 field.backgroundColor = NSColor.selectedTextBackgroundColor;
211 return [super becomeFirstResponder];
212 }
213
214 - (BOOL)resignFirstResponder {
215 field.backgroundColor = NSColor.textBackgroundColor;
216 return [super resignFirstResponder];
217 }
218
219 - (void)setKeyCode:(CGKeyCode)keyCode {
220 _keyCode = keyCode;
221 field.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
222 }
223
224 - (void)keyDown:(NSEvent *)event {
225 static const NSUInteger IGNORE = NSAlternateKeyMask | NSCommandKeyMask;
226 if (!event.isARepeat) {
227 if ((event.modifierFlags & IGNORE) && event.keyCode == kVK_Delete) {
228 // Allow Alt/Command+Delete to clear the field.
229 self.keyCode = NJKeyInputFieldEmpty;
230 [self.delegate keyInputFieldDidClear:self];
231 } else if (!(event.modifierFlags & IGNORE)) {
232 self.keyCode = event.keyCode;
233 [self.delegate keyInputField:self didChangeKey:self.keyCode];
234 }
235 [self resignIfFirstResponder];
236 }
237 }
238
239 static BOOL isValidKeyCode(long code) {
240 return code < 0xFFFF && code >= 0;
241 }
242
243 - (void)controlTextDidChange:(NSNotification *)obj {
244 char *error = NULL;
245 long code = strtol(field.stringValue.UTF8String, &error, 16);
246 warning.hidden = (isValidKeyCode(code) && !*error) || !field.stringValue.length;
247 }
248
249 - (void)controlTextDidEndEditing:(NSNotification *)obj {
250 [field.cell setPlaceholderString:@""];
251 field.editable = NO;
252 field.selectable = NO;
253 warning.hidden = YES;
254 char *error = NULL;
255 const char *s = field.stringValue.UTF8String;
256 long code = strtol(s, &error, 16);
257
258 if (!*error && isValidKeyCode(code) && field.stringValue.length) {
259 self.keyCode = code;
260 [self.delegate keyInputField:self didChangeKey:self.keyCode];
261 } else {
262 self.keyCode = self.keyCode;
263 }
264 }
265
266 - (void)mouseDown:(NSEvent *)theEvent {
267 if (self.isEnabled) {
268 if (theEvent.modifierFlags & NSCommandKeyMask) {
269 field.editable = YES;
270 field.selectable = YES;
271 field.stringValue = @"";
272 [field.cell setPlaceholderString:
273 NSLocalizedString(@"enter key code",
274 @"shown when user must enter a key code to map to")];
275 [self.window makeFirstResponder:field];
276 } else {
277 if (self.window.firstResponder == self)
278 [self.window makeFirstResponder:nil];
279 else if (self.acceptsFirstResponder)
280 [self.window makeFirstResponder:self];
281 }
282 }
283 }
284
285 - (void)flagsChanged:(NSEvent *)theEvent {
286 // Many keys are only available on MacBook keyboards by using the
287 // Fn modifier key (e.g. Fn+Left for Home), so delay processing
288 // modifiers until the up event is received in order to let the
289 // user type these virtual keys. However, there is no actual event
290 // for modifier key up - so detect it by checking to see if any
291 // modifiers are still down.
292 if (!field.isEditable
293 && !(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) {
294 self.keyCode = theEvent.keyCode;
295 [self.delegate keyInputField:self didChangeKey:_keyCode];
296 }
297 }
298
299 @end