Support sudden and automatic termination.
[enjoyable.git] / NJKeyInputField.m
1 //
2 // NJKeyInputField.h
3 // Enjoyable
4 //
5 // Copyright 2013 Joe Wreschnig.
6 //
7
8 #import "NJKeyInputField.h"
9
10 CGKeyCode NJKeyInputFieldEmpty = 0xFFFF;
11
12 @implementation NJKeyInputField
13
14 - (id)initWithFrame:(NSRect)frameRect {
15 if ((self = [super initWithFrame:frameRect])) {
16 self.alignment = NSCenterTextAlignment;
17 [self setEditable:NO];
18 [self setSelectable:NO];
19 }
20 return self;
21 }
22
23 - (void)clear {
24 self.keyCode = NJKeyInputFieldEmpty;
25 [self.keyDelegate keyInputFieldDidClear:self];
26 [self resignIfFirstResponder];
27 }
28
29 - (BOOL)hasKeyCode {
30 return self.keyCode != NJKeyInputFieldEmpty;
31 }
32
33 + (NSString *)stringForKeyCode:(CGKeyCode)keyCode {
34 switch (keyCode) {
35 case 0xffff: return @"";
36 case 0x7a: return @"F1";
37 case 0x78: return @"F2";
38 case 0x63: return @"F3";
39 case 0x76: return @"F4";
40 case 0x60: return @"F5";
41 case 0x61: return @"F6";
42 case 0x62: return @"F7";
43 case 0x64: return @"F8";
44 case 0x65: return @"F9";
45 case 0x6d: return @"F10";
46 case 0x67: return @"F11";
47 case 0x6f: return @"F12";
48 case 0x69: return @"F13";
49 case 0x6b: return @"F14";
50 case 0x71: return @"F15";
51 case 0x6a: return @"F16";
52 case 0x40: return @"F17";
53 case 0x4f: return @"F18";
54 case 0x50: return @"F19";
55
56 case 0x35: return @"Esc";
57 case 0x32: return @"`";
58
59 case 0x12: return @"1";
60 case 0x13: return @"2";
61 case 0x14: return @"3";
62 case 0x15: return @"4";
63 case 0x17: return @"5";
64 case 0x16: return @"6";
65 case 0x1a: return @"7";
66 case 0x1c: return @"8";
67 case 0x19: return @"9";
68 case 0x1d: return @"0";
69 case 0x1b: return @"-";
70 case 0x18: return @"=";
71
72 case 0x3f: return @"Fn";
73 case 0x36: return @"Right Command";
74 case 0x37: return @"Left Command";
75 case 0x38: return @"Left Shift";
76 case 0x39: return @"Caps Lock";
77 case 0x3a: return @"Left Option";
78 case 0x3b: return @"Left Control";
79 case 0x3c: return @"Right Shift";
80 case 0x3d: return @"Right Option";
81 case 0x3e: return @"Right Control";
82
83 case 0x73: return @"Home";
84 case 0x74: return @"Page Up";
85 case 0x75: return @"Delete";
86 case 0x77: return @"End";
87 case 0x79: return @"Page Down";
88
89 case 0x30: return @"Tab";
90 case 0x33: return @"Backspace";
91 case 0x24: return @"Return";
92 case 0x31: return @"Space";
93
94 case 0x0c: return @"Q";
95 case 0x0d: return @"W";
96 case 0x0e: return @"E";
97 case 0x0f: return @"R";
98 case 0x11: return @"T";
99 case 0x10: return @"Y";
100 case 0x20: return @"U";
101 case 0x22: return @"I";
102 case 0x1f: return @"O";
103 case 0x23: return @"P";
104 case 0x21: return @"[";
105 case 0x1e: return @"]";
106 case 0x2a: return @"\\";
107 case 0x00: return @"A";
108 case 0x01: return @"S";
109 case 0x02: return @"D";
110 case 0x03: return @"F";
111 case 0x05: return @"G";
112 case 0x04: return @"H";
113 case 0x26: return @"J";
114 case 0x28: return @"K";
115 case 0x25: return @"L";
116 case 0x29: return @";";
117 case 0x27: return @"'";
118 case 0x06: return @"Z";
119 case 0x07: return @"X";
120 case 0x08: return @"C";
121 case 0x09: return @"V";
122 case 0x0b: return @"B";
123 case 0x2d: return @"N";
124 case 0x2e: return @"M";
125 case 0x2b: return @",";
126 case 0x2f: return @".";
127 case 0x2c: return @"/";
128
129 case 0x47: return @"Clear";
130 case 0x51: return @"Keypad =";
131 case 0x4b: return @"Keypad /";
132 case 0x43: return @"Keypad *";
133 case 0x59: return @"Keypad 7";
134 case 0x5b: return @"Keypad 8";
135 case 0x5c: return @"Keypad 9";
136 case 0x4e: return @"Keypad -";
137 case 0x56: return @"Keypad 4";
138 case 0x57: return @"Keypad 5";
139 case 0x58: return @"Keypad 6";
140 case 0x45: return @"Keypad +";
141 case 0x53: return @"Keypad 1";
142 case 0x54: return @"Keypad 2";
143 case 0x55: return @"Keypad 3";
144 case 0x52: return @"Keypad 0";
145 case 0x41: return @"Keypad .";
146 case 0x4c: return @"Enter";
147
148 case 0x7e: return @"Up";
149 case 0x7d: return @"Down";
150 case 0x7b: return @"Left";
151 case 0x7c: return @"Right";
152 default:
153 return [[NSString alloc] initWithFormat:@"Key 0x%x", keyCode];
154 }
155 }
156
157 - (BOOL)acceptsFirstResponder {
158 return self.isEnabled;
159 }
160
161 - (BOOL)becomeFirstResponder {
162 self.backgroundColor = NSColor.selectedTextBackgroundColor;
163 return [super becomeFirstResponder];
164 }
165
166 - (BOOL)resignFirstResponder {
167 self.backgroundColor = NSColor.textBackgroundColor;
168 return [super resignFirstResponder];
169 }
170
171 - (void)setKeyCode:(CGKeyCode)keyCode {
172 _keyCode = keyCode;
173 self.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
174 }
175
176 - (void)keyDown:(NSEvent *)theEvent {
177 if (!theEvent.isARepeat) {
178 if ((theEvent.modifierFlags & NSAlternateKeyMask)
179 && theEvent.keyCode == 0x35) {
180 // Allow Alt+Escape to clear the field.
181 self.keyCode = NJKeyInputFieldEmpty;
182 [self.keyDelegate keyInputFieldDidClear:self];
183 } else {
184 self.keyCode = theEvent.keyCode;
185 [self.keyDelegate keyInputField:self didChangeKey:_keyCode];
186 }
187 [self resignIfFirstResponder];
188 }
189 }
190
191 - (void)mouseDown:(NSEvent *)theEvent {
192 if (self.acceptsFirstResponder)
193 [self.window makeFirstResponder:self];
194 }
195
196 - (void)flagsChanged:(NSEvent *)theEvent {
197 // Many keys are only available on MacBook keyboards by using the
198 // Fn modifier key (e.g. Fn+Left for Home), so delay processing
199 // modifiers until the up event is received in order to let the
200 // user type these virtual keys. However, there is no actual event
201 // for modifier key up - so detect it by checking to see if any
202 // modifiers are still down.
203 if (!(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) {
204 self.keyCode = theEvent.keyCode;
205 [self.keyDelegate keyInputField:self didChangeKey:_keyCode];
206 }
207 }
208
209 @end