Debian Codegraph @

platform/macos/key_mapping_macos.mm

raw
1 /**************************************************************************/
2 /* key_mapping_macos.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "key_mapping_macos.h"
32
33 #include "core/templates/hash_map.h"
34 #include "core/templates/hash_set.h"
35
36 #import <Carbon/Carbon.h>
37 #import <Cocoa/Cocoa.h>
38
39 struct HashMapHasherKeys {
40 static _FORCE_INLINE_ uint32_t hash(const Key p_key) { return hash_fmix32(static_cast<uint32_t>(p_key)); }
41 static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(p_uchar); }
42 static _FORCE_INLINE_ uint32_t hash(const unsigned p_key) { return hash_fmix32(p_key); }
43 };
44
45 HashSet<unsigned int> numpad_keys;
46 HashMap<unsigned int, Key, HashMapHasherKeys> keysym_map;
47 HashMap<Key, unsigned int, HashMapHasherKeys> keysym_map_inv;
48 HashMap<Key, char32_t, HashMapHasherKeys> keycode_map;
49 HashMap<unsigned int, KeyLocation, HashMapHasherKeys> location_map;
50
51 void KeyMappingMacOS::initialize() {
52 numpad_keys.insert(0x41); //kVK_ANSI_KeypadDecimal
53 numpad_keys.insert(0x43); //kVK_ANSI_KeypadMultiply
54 numpad_keys.insert(0x45); //kVK_ANSI_KeypadPlus
55 numpad_keys.insert(0x47); //kVK_ANSI_KeypadClear
56 numpad_keys.insert(0x4b); //kVK_ANSI_KeypadDivide
57 numpad_keys.insert(0x4c); //kVK_ANSI_KeypadEnter
58 numpad_keys.insert(0x4e); //kVK_ANSI_KeypadMinus
59 numpad_keys.insert(0x51); //kVK_ANSI_KeypadEquals
60 numpad_keys.insert(0x52); //kVK_ANSI_Keypad0
61 numpad_keys.insert(0x53); //kVK_ANSI_Keypad1
62 numpad_keys.insert(0x54); //kVK_ANSI_Keypad2
63 numpad_keys.insert(0x55); //kVK_ANSI_Keypad3
64 numpad_keys.insert(0x56); //kVK_ANSI_Keypad4
65 numpad_keys.insert(0x57); //kVK_ANSI_Keypad5
66 numpad_keys.insert(0x58); //kVK_ANSI_Keypad6
67 numpad_keys.insert(0x59); //kVK_ANSI_Keypad7
68 numpad_keys.insert(0x5b); //kVK_ANSI_Keypad8
69 numpad_keys.insert(0x5c); //kVK_ANSI_Keypad9
70 numpad_keys.insert(0x5f); //kVK_JIS_KeypadComma
71
72 keysym_map[0x00] = Key::A;
73 keysym_map[0x01] = Key::S;
74 keysym_map[0x02] = Key::D;
75 keysym_map[0x03] = Key::F;
76 keysym_map[0x04] = Key::H;
77 keysym_map[0x05] = Key::G;
78 keysym_map[0x06] = Key::Z;
79 keysym_map[0x07] = Key::X;
80 keysym_map[0x08] = Key::C;
81 keysym_map[0x09] = Key::V;
82 keysym_map[0x0a] = Key::SECTION;
83 keysym_map[0x0b] = Key::B;
84 keysym_map[0x0c] = Key::Q;
85 keysym_map[0x0d] = Key::W;
86 keysym_map[0x0e] = Key::E;
87 keysym_map[0x0f] = Key::R;
88 keysym_map[0x10] = Key::Y;
89 keysym_map[0x11] = Key::T;
90 keysym_map[0x12] = Key::KEY_1;
91 keysym_map[0x13] = Key::KEY_2;
92 keysym_map[0x14] = Key::KEY_3;
93 keysym_map[0x15] = Key::KEY_4;
94 keysym_map[0x16] = Key::KEY_6;
95 keysym_map[0x17] = Key::KEY_5;
96 keysym_map[0x18] = Key::EQUAL;
97 keysym_map[0x19] = Key::KEY_9;
98 keysym_map[0x1a] = Key::KEY_7;
99 keysym_map[0x1b] = Key::MINUS;
100 keysym_map[0x1c] = Key::KEY_8;
101 keysym_map[0x1d] = Key::KEY_0;
102 keysym_map[0x1e] = Key::BRACKETRIGHT;
103 keysym_map[0x1f] = Key::O;
104 keysym_map[0x20] = Key::U;
105 keysym_map[0x21] = Key::BRACKETLEFT;
106 keysym_map[0x22] = Key::I;
107 keysym_map[0x23] = Key::P;
108 keysym_map[0x24] = Key::ENTER;
109 keysym_map[0x25] = Key::L;
110 keysym_map[0x26] = Key::J;
111 keysym_map[0x27] = Key::APOSTROPHE;
112 keysym_map[0x28] = Key::K;
113 keysym_map[0x29] = Key::SEMICOLON;
114 keysym_map[0x2a] = Key::BACKSLASH;
115 keysym_map[0x2b] = Key::COMMA;
116 keysym_map[0x2c] = Key::SLASH;
117 keysym_map[0x2d] = Key::N;
118 keysym_map[0x2e] = Key::M;
119 keysym_map[0x2f] = Key::PERIOD;
120 keysym_map[0x30] = Key::TAB;
121 keysym_map[0x31] = Key::SPACE;
122 keysym_map[0x32] = Key::QUOTELEFT;
123 keysym_map[0x33] = Key::BACKSPACE;
124 keysym_map[0x35] = Key::ESCAPE;
125 keysym_map[0x36] = Key::META;
126 keysym_map[0x37] = Key::META;
127 keysym_map[0x38] = Key::SHIFT;
128 keysym_map[0x39] = Key::CAPSLOCK;
129 keysym_map[0x3a] = Key::ALT;
130 keysym_map[0x3b] = Key::CTRL;
131 keysym_map[0x3c] = Key::SHIFT;
132 keysym_map[0x3d] = Key::ALT;
133 keysym_map[0x3e] = Key::CTRL;
134 keysym_map[0x40] = Key::F17;
135 keysym_map[0x41] = Key::KP_PERIOD;
136 keysym_map[0x43] = Key::KP_MULTIPLY;
137 keysym_map[0x45] = Key::KP_ADD;
138 keysym_map[0x47] = Key::NUMLOCK;
139 keysym_map[0x48] = Key::VOLUMEUP;
140 keysym_map[0x49] = Key::VOLUMEDOWN;
141 keysym_map[0x4a] = Key::VOLUMEMUTE;
142 keysym_map[0x4b] = Key::KP_DIVIDE;
143 keysym_map[0x4c] = Key::KP_ENTER;
144 keysym_map[0x4e] = Key::KP_SUBTRACT;
145 keysym_map[0x4f] = Key::F18;
146 keysym_map[0x50] = Key::F19;
147 keysym_map[0x51] = Key::EQUAL;
148 keysym_map[0x52] = Key::KP_0;
149 keysym_map[0x53] = Key::KP_1;
150 keysym_map[0x54] = Key::KP_2;
151 keysym_map[0x55] = Key::KP_3;
152 keysym_map[0x56] = Key::KP_4;
153 keysym_map[0x57] = Key::KP_5;
154 keysym_map[0x58] = Key::KP_6;
155 keysym_map[0x59] = Key::KP_7;
156 keysym_map[0x5a] = Key::F20;
157 keysym_map[0x5b] = Key::KP_8;
158 keysym_map[0x5c] = Key::KP_9;
159 keysym_map[0x5d] = Key::YEN;
160 keysym_map[0x5e] = Key::UNDERSCORE;
161 keysym_map[0x5f] = Key::COMMA;
162 keysym_map[0x60] = Key::F5;
163 keysym_map[0x61] = Key::F6;
164 keysym_map[0x62] = Key::F7;
165 keysym_map[0x63] = Key::F3;
166 keysym_map[0x64] = Key::F8;
167 keysym_map[0x65] = Key::F9;
168 keysym_map[0x66] = Key::JIS_EISU;
169 keysym_map[0x67] = Key::F11;
170 keysym_map[0x68] = Key::JIS_KANA;
171 keysym_map[0x69] = Key::F13;
172 keysym_map[0x6a] = Key::F16;
173 keysym_map[0x6b] = Key::F14;
174 keysym_map[0x6d] = Key::F10;
175 keysym_map[0x6e] = Key::MENU;
176 keysym_map[0x6f] = Key::F12;
177 keysym_map[0x71] = Key::F15;
178 keysym_map[0x72] = Key::INSERT;
179 keysym_map[0x73] = Key::HOME;
180 keysym_map[0x74] = Key::PAGEUP;
181 keysym_map[0x75] = Key::KEY_DELETE;
182 keysym_map[0x76] = Key::F4;
183 keysym_map[0x77] = Key::END;
184 keysym_map[0x78] = Key::F2;
185 keysym_map[0x79] = Key::PAGEDOWN;
186 keysym_map[0x7a] = Key::F1;
187 keysym_map[0x7b] = Key::LEFT;
188 keysym_map[0x7c] = Key::RIGHT;
189 keysym_map[0x7d] = Key::DOWN;
190 keysym_map[0x7e] = Key::UP;
191
192 for (const KeyValue<unsigned int, Key> &E : keysym_map) {
193 keysym_map_inv[E.value] = E.key;
194 }
195
196 keycode_map[Key::ESCAPE] = 0x001B;
197 keycode_map[Key::TAB] = 0x0009;
198 keycode_map[Key::BACKTAB] = 0x007F;
199 keycode_map[Key::BACKSPACE] = 0x0008;
200 keycode_map[Key::ENTER] = 0x000D;
201 keycode_map[Key::INSERT] = NSInsertFunctionKey;
202 keycode_map[Key::KEY_DELETE] = 0x007F;
203 keycode_map[Key::PAUSE] = NSPauseFunctionKey;
204 keycode_map[Key::PRINT] = NSPrintScreenFunctionKey;
205 keycode_map[Key::SYSREQ] = NSSysReqFunctionKey;
206 keycode_map[Key::CLEAR] = NSClearLineFunctionKey;
207 keycode_map[Key::HOME] = 0x2196;
208 keycode_map[Key::END] = 0x2198;
209 keycode_map[Key::LEFT] = 0x001C;
210 keycode_map[Key::UP] = 0x001E;
211 keycode_map[Key::RIGHT] = 0x001D;
212 keycode_map[Key::DOWN] = 0x001F;
213 keycode_map[Key::PAGEUP] = 0x21DE;
214 keycode_map[Key::PAGEDOWN] = 0x21DF;
215 keycode_map[Key::NUMLOCK] = NSClearLineFunctionKey;
216 keycode_map[Key::SCROLLLOCK] = NSScrollLockFunctionKey;
217 keycode_map[Key::F1] = NSF1FunctionKey;
218 keycode_map[Key::F2] = NSF2FunctionKey;
219 keycode_map[Key::F3] = NSF3FunctionKey;
220 keycode_map[Key::F4] = NSF4FunctionKey;
221 keycode_map[Key::F5] = NSF5FunctionKey;
222 keycode_map[Key::F6] = NSF6FunctionKey;
223 keycode_map[Key::F7] = NSF7FunctionKey;
224 keycode_map[Key::F8] = NSF8FunctionKey;
225 keycode_map[Key::F9] = NSF9FunctionKey;
226 keycode_map[Key::F10] = NSF10FunctionKey;
227 keycode_map[Key::F11] = NSF11FunctionKey;
228 keycode_map[Key::F12] = NSF12FunctionKey;
229 keycode_map[Key::F13] = NSF13FunctionKey;
230 keycode_map[Key::F14] = NSF14FunctionKey;
231 keycode_map[Key::F15] = NSF15FunctionKey;
232 keycode_map[Key::F16] = NSF16FunctionKey;
233 keycode_map[Key::F17] = NSF17FunctionKey;
234 keycode_map[Key::F18] = NSF18FunctionKey;
235 keycode_map[Key::F19] = NSF19FunctionKey;
236 keycode_map[Key::F20] = NSF20FunctionKey;
237 keycode_map[Key::F21] = NSF21FunctionKey;
238 keycode_map[Key::F22] = NSF22FunctionKey;
239 keycode_map[Key::F23] = NSF23FunctionKey;
240 keycode_map[Key::F24] = NSF24FunctionKey;
241 keycode_map[Key::F25] = NSF25FunctionKey;
242 keycode_map[Key::F26] = NSF26FunctionKey;
243 keycode_map[Key::F27] = NSF27FunctionKey;
244 keycode_map[Key::F28] = NSF28FunctionKey;
245 keycode_map[Key::F29] = NSF29FunctionKey;
246 keycode_map[Key::F30] = NSF30FunctionKey;
247 keycode_map[Key::F31] = NSF31FunctionKey;
248 keycode_map[Key::F32] = NSF32FunctionKey;
249 keycode_map[Key::F33] = NSF33FunctionKey;
250 keycode_map[Key::F34] = NSF34FunctionKey;
251 keycode_map[Key::F35] = NSF35FunctionKey;
252 keycode_map[Key::MENU] = NSMenuFunctionKey;
253 keycode_map[Key::HELP] = NSHelpFunctionKey;
254 keycode_map[Key::STOP] = NSStopFunctionKey;
255 keycode_map[Key::LAUNCH0] = NSUserFunctionKey;
256 keycode_map[Key::SPACE] = 0x0020;
257 keycode_map[Key::EXCLAM] = '!';
258 keycode_map[Key::QUOTEDBL] = '\"';
259 keycode_map[Key::NUMBERSIGN] = '#';
260 keycode_map[Key::DOLLAR] = '$';
261 keycode_map[Key::PERCENT] = '\%';
262 keycode_map[Key::AMPERSAND] = '&';
263 keycode_map[Key::APOSTROPHE] = '\'';
264 keycode_map[Key::PARENLEFT] = '(';
265 keycode_map[Key::PARENRIGHT] = ')';
266 keycode_map[Key::ASTERISK] = '*';
267 keycode_map[Key::PLUS] = '+';
268 keycode_map[Key::COMMA] = ',';
269 keycode_map[Key::MINUS] = '-';
270 keycode_map[Key::PERIOD] = '.';
271 keycode_map[Key::SLASH] = '/';
272 keycode_map[Key::KEY_0] = '0';
273 keycode_map[Key::KEY_1] = '1';
274 keycode_map[Key::KEY_2] = '2';
275 keycode_map[Key::KEY_3] = '3';
276 keycode_map[Key::KEY_4] = '4';
277 keycode_map[Key::KEY_5] = '5';
278 keycode_map[Key::KEY_6] = '6';
279 keycode_map[Key::KEY_7] = '7';
280 keycode_map[Key::KEY_8] = '8';
281 keycode_map[Key::KEY_9] = '9';
282 keycode_map[Key::COLON] = ':';
283 keycode_map[Key::SEMICOLON] = ';';
284 keycode_map[Key::LESS] = '<';
285 keycode_map[Key::EQUAL] = '=';
286 keycode_map[Key::GREATER] = '>';
287 keycode_map[Key::QUESTION] = '?';
288 keycode_map[Key::AT] = '@';
289 keycode_map[Key::A] = 'a';
290 keycode_map[Key::B] = 'b';
291 keycode_map[Key::C] = 'c';
292 keycode_map[Key::D] = 'd';
293 keycode_map[Key::E] = 'e';
294 keycode_map[Key::F] = 'f';
295 keycode_map[Key::G] = 'g';
296 keycode_map[Key::H] = 'h';
297 keycode_map[Key::I] = 'i';
298 keycode_map[Key::J] = 'j';
299 keycode_map[Key::K] = 'k';
300 keycode_map[Key::L] = 'l';
301 keycode_map[Key::M] = 'm';
302 keycode_map[Key::N] = 'n';
303 keycode_map[Key::O] = 'o';
304 keycode_map[Key::P] = 'p';
305 keycode_map[Key::Q] = 'q';
306 keycode_map[Key::R] = 'r';
307 keycode_map[Key::S] = 's';
308 keycode_map[Key::T] = 't';
309 keycode_map[Key::U] = 'u';
310 keycode_map[Key::V] = 'v';
311 keycode_map[Key::W] = 'w';
312 keycode_map[Key::X] = 'x';
313 keycode_map[Key::Y] = 'y';
314 keycode_map[Key::Z] = 'z';
315 keycode_map[Key::BRACKETLEFT] = '[';
316 keycode_map[Key::BACKSLASH] = '\\';
317 keycode_map[Key::BRACKETRIGHT] = ']';
318 keycode_map[Key::ASCIICIRCUM] = '^';
319 keycode_map[Key::UNDERSCORE] = '_';
320 keycode_map[Key::QUOTELEFT] = '`';
321 keycode_map[Key::BRACELEFT] = '{';
322 keycode_map[Key::BAR] = '|';
323 keycode_map[Key::BRACERIGHT] = '}';
324 keycode_map[Key::ASCIITILDE] = '~';
325
326 // Keysym -> physical location.
327 // Ctrl.
328 location_map[0x3b] = KeyLocation::LEFT;
329 location_map[0x3e] = KeyLocation::RIGHT;
330 // Shift.
331 location_map[0x38] = KeyLocation::LEFT;
332 location_map[0x3c] = KeyLocation::RIGHT;
333 // Alt/Option.
334 location_map[0x3a] = KeyLocation::LEFT;
335 location_map[0x3d] = KeyLocation::RIGHT;
336 // Meta/Command (yes, right < left).
337 location_map[0x36] = KeyLocation::RIGHT;
338 location_map[0x37] = KeyLocation::LEFT;
339 }
340
341 bool KeyMappingMacOS::is_numpad_key(unsigned int p_key) {
342 return numpad_keys.has(p_key);
343 }
344
345 // Translates a macOS keycode to a Godot keycode.
346 Key KeyMappingMacOS::translate_key(unsigned int p_key) {
347 const Key *key = keysym_map.getptr(p_key);
348 if (key) {
349 return *key;
350 }
351 return Key::NONE;
352 }
353
354 // Translates a Godot keycode back to a macOS keycode.
355 unsigned int KeyMappingMacOS::unmap_key(Key p_key) {
356 const unsigned int *key = keysym_map_inv.getptr(p_key);
357 if (key) {
358 return *key;
359 }
360 return 127;
361 }
362
363 // Remap key according to current keyboard layout.
364 Key KeyMappingMacOS::remap_key(unsigned int p_key, unsigned int p_state, bool p_unicode) {
365 if (is_numpad_key(p_key)) {
366 return translate_key(p_key);
367 }
368
369 TISInputSourceRef current_keyboard = TISCopyCurrentKeyboardInputSource();
370 if (!current_keyboard) {
371 return translate_key(p_key);
372 }
373
374 CFDataRef layout_data = (CFDataRef)TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData);
375 if (!layout_data) {
376 return translate_key(p_key);
377 }
378
379 const UCKeyboardLayout *keyboard_layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);
380
381 String keysym;
382 UInt32 keys_down = 0;
383 UniChar chars[256] = {};
384 UniCharCount real_length = 0;
385
386 OSStatus err = UCKeyTranslate(keyboard_layout,
387 p_key,
388 kUCKeyActionDisplay,
389 (p_unicode) ? 0 : (p_state >> 8) & 0xFF,
390 LMGetKbdType(),
391 kUCKeyTranslateNoDeadKeysBit,
392 &keys_down,
393 std_size(chars),
394 &real_length,
395 chars);
396
397 if (err != noErr) {
398 return translate_key(p_key);
399 }
400
401 keysym = String::utf16((char16_t *)chars, real_length);
402 if (keysym.is_empty()) {
403 return translate_key(p_key);
404 }
405
406 char32_t c = keysym[0];
407 if (p_unicode) {
408 return fix_key_label(c, translate_key(p_key));
409 } else {
410 return fix_keycode(c, translate_key(p_key));
411 }
412 }
413
414 // Translates a macOS keycode to a Godot key location.
415 KeyLocation KeyMappingMacOS::translate_location(unsigned int p_key) {
416 const KeyLocation *location = location_map.getptr(p_key);
417 if (location) {
418 return *location;
419 }
420 return KeyLocation::UNSPECIFIED;
421 }
422
423 String KeyMappingMacOS::keycode_get_native_string(Key p_keycode) {
424 const char32_t *key = keycode_map.getptr(p_keycode);
425 if (key) {
426 return String::chr(*key);
427 }
428 return String();
429 }
430
431 unsigned int KeyMappingMacOS::keycode_get_native_mask(Key p_keycode) {
432 unsigned int mask = 0;
433 if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) {
434 mask |= NSEventModifierFlagControl;
435 }
436 if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) {
437 mask |= NSEventModifierFlagOption;
438 }
439 if ((p_keycode & KeyModifierMask::SHIFT) != Key::NONE) {
440 mask |= NSEventModifierFlagShift;
441 }
442 if ((p_keycode & KeyModifierMask::META) != Key::NONE) {
443 mask |= NSEventModifierFlagCommand;
444 }
445 if ((p_keycode & KeyModifierMask::KPAD) != Key::NONE) {
446 mask |= NSEventModifierFlagNumericPad;
447 }
448 return mask;
449 }