summaryrefslogtreecommitdiff
path: root/platform/macos/key_mapping_macos.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/macos/key_mapping_macos.mm')
-rw-r--r--platform/macos/key_mapping_macos.mm717
1 files changed, 323 insertions, 394 deletions
diff --git a/platform/macos/key_mapping_macos.mm b/platform/macos/key_mapping_macos.mm
index 478c84e81c..31b71ac1b8 100644
--- a/platform/macos/key_mapping_macos.mm
+++ b/platform/macos/key_mapping_macos.mm
@@ -33,277 +33,345 @@
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
-bool KeyMappingMacOS::is_numpad_key(unsigned int key) {
- static const unsigned int table[] = {
- 0x41, /* kVK_ANSI_KeypadDecimal */
- 0x43, /* kVK_ANSI_KeypadMultiply */
- 0x45, /* kVK_ANSI_KeypadPlus */
- 0x47, /* kVK_ANSI_KeypadClear */
- 0x4b, /* kVK_ANSI_KeypadDivide */
- 0x4c, /* kVK_ANSI_KeypadEnter */
- 0x4e, /* kVK_ANSI_KeypadMinus */
- 0x51, /* kVK_ANSI_KeypadEquals */
- 0x52, /* kVK_ANSI_Keypad0 */
- 0x53, /* kVK_ANSI_Keypad1 */
- 0x54, /* kVK_ANSI_Keypad2 */
- 0x55, /* kVK_ANSI_Keypad3 */
- 0x56, /* kVK_ANSI_Keypad4 */
- 0x57, /* kVK_ANSI_Keypad5 */
- 0x58, /* kVK_ANSI_Keypad6 */
- 0x59, /* kVK_ANSI_Keypad7 */
- 0x5b, /* kVK_ANSI_Keypad8 */
- 0x5c, /* kVK_ANSI_Keypad9 */
- 0x5f, /* kVK_JIS_KeypadComma */
- 0x00
- };
- for (int i = 0; table[i] != 0; i++) {
- if (key == table[i]) {
- return true;
- }
+#include "core/templates/hash_map.h"
+#include "core/templates/hash_set.h"
+
+struct HashMapHasherKeys {
+ static _FORCE_INLINE_ uint32_t hash(const Key p_key) { return hash_fmix32(static_cast<uint32_t>(p_key)); }
+ static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(p_uchar); }
+ static _FORCE_INLINE_ uint32_t hash(const unsigned p_key) { return hash_fmix32(p_key); }
+};
+
+HashSet<unsigned int> numpad_keys;
+HashMap<unsigned int, Key, HashMapHasherKeys> keysym_map;
+HashMap<Key, unsigned int, HashMapHasherKeys> keysym_map_inv;
+HashMap<Key, char32_t, HashMapHasherKeys> keycode_map;
+
+void KeyMappingMacOS::initialize() {
+ numpad_keys.insert(0x41); //kVK_ANSI_KeypadDecimal
+ numpad_keys.insert(0x43); //kVK_ANSI_KeypadMultiply
+ numpad_keys.insert(0x45); //kVK_ANSI_KeypadPlus
+ numpad_keys.insert(0x47); //kVK_ANSI_KeypadClear
+ numpad_keys.insert(0x4b); //kVK_ANSI_KeypadDivide
+ numpad_keys.insert(0x4c); //kVK_ANSI_KeypadEnter
+ numpad_keys.insert(0x4e); //kVK_ANSI_KeypadMinus
+ numpad_keys.insert(0x51); //kVK_ANSI_KeypadEquals
+ numpad_keys.insert(0x52); //kVK_ANSI_Keypad0
+ numpad_keys.insert(0x53); //kVK_ANSI_Keypad1
+ numpad_keys.insert(0x54); //kVK_ANSI_Keypad2
+ numpad_keys.insert(0x55); //kVK_ANSI_Keypad3
+ numpad_keys.insert(0x56); //kVK_ANSI_Keypad4
+ numpad_keys.insert(0x57); //kVK_ANSI_Keypad5
+ numpad_keys.insert(0x58); //kVK_ANSI_Keypad6
+ numpad_keys.insert(0x59); //kVK_ANSI_Keypad7
+ numpad_keys.insert(0x5b); //kVK_ANSI_Keypad8
+ numpad_keys.insert(0x5c); //kVK_ANSI_Keypad9
+ numpad_keys.insert(0x5f); //kVK_JIS_KeypadComma
+
+ keysym_map[0x00] = Key::A;
+ keysym_map[0x01] = Key::S;
+ keysym_map[0x02] = Key::D;
+ keysym_map[0x03] = Key::F;
+ keysym_map[0x04] = Key::H;
+ keysym_map[0x05] = Key::G;
+ keysym_map[0x06] = Key::Z;
+ keysym_map[0x07] = Key::X;
+ keysym_map[0x08] = Key::C;
+ keysym_map[0x09] = Key::V;
+ keysym_map[0x0a] = Key::SECTION;
+ keysym_map[0x0b] = Key::B;
+ keysym_map[0x0c] = Key::Q;
+ keysym_map[0x0d] = Key::W;
+ keysym_map[0x0e] = Key::E;
+ keysym_map[0x0f] = Key::R;
+ keysym_map[0x10] = Key::Y;
+ keysym_map[0x11] = Key::T;
+ keysym_map[0x12] = Key::KEY_1;
+ keysym_map[0x13] = Key::KEY_2;
+ keysym_map[0x14] = Key::KEY_3;
+ keysym_map[0x15] = Key::KEY_4;
+ keysym_map[0x16] = Key::KEY_6;
+ keysym_map[0x17] = Key::KEY_5;
+ keysym_map[0x18] = Key::EQUAL;
+ keysym_map[0x19] = Key::KEY_9;
+ keysym_map[0x1a] = Key::KEY_7;
+ keysym_map[0x1b] = Key::MINUS;
+ keysym_map[0x1c] = Key::KEY_8;
+ keysym_map[0x1d] = Key::KEY_0;
+ keysym_map[0x1e] = Key::BRACERIGHT;
+ keysym_map[0x1f] = Key::O;
+ keysym_map[0x20] = Key::U;
+ keysym_map[0x21] = Key::BRACELEFT;
+ keysym_map[0x22] = Key::I;
+ keysym_map[0x23] = Key::P;
+ keysym_map[0x24] = Key::ENTER;
+ keysym_map[0x25] = Key::L;
+ keysym_map[0x26] = Key::J;
+ keysym_map[0x27] = Key::APOSTROPHE;
+ keysym_map[0x28] = Key::K;
+ keysym_map[0x29] = Key::SEMICOLON;
+ keysym_map[0x2a] = Key::BACKSLASH;
+ keysym_map[0x2b] = Key::COMMA;
+ keysym_map[0x2c] = Key::SLASH;
+ keysym_map[0x2d] = Key::N;
+ keysym_map[0x2e] = Key::M;
+ keysym_map[0x2f] = Key::PERIOD;
+ keysym_map[0x30] = Key::TAB;
+ keysym_map[0x31] = Key::SPACE;
+ keysym_map[0x32] = Key::QUOTELEFT;
+ keysym_map[0x33] = Key::BACKSPACE;
+ keysym_map[0x35] = Key::ESCAPE;
+ keysym_map[0x36] = Key::META;
+ keysym_map[0x37] = Key::META;
+ keysym_map[0x38] = Key::SHIFT;
+ keysym_map[0x39] = Key::CAPSLOCK;
+ keysym_map[0x3a] = Key::ALT;
+ keysym_map[0x3b] = Key::CTRL;
+ keysym_map[0x3c] = Key::SHIFT;
+ keysym_map[0x3d] = Key::ALT;
+ keysym_map[0x3e] = Key::CTRL;
+ keysym_map[0x40] = Key::F17;
+ keysym_map[0x41] = Key::KP_PERIOD;
+ keysym_map[0x43] = Key::KP_MULTIPLY;
+ keysym_map[0x45] = Key::KP_ADD;
+ keysym_map[0x47] = Key::NUMLOCK;
+ keysym_map[0x48] = Key::VOLUMEUP;
+ keysym_map[0x49] = Key::VOLUMEDOWN;
+ keysym_map[0x4a] = Key::VOLUMEMUTE;
+ keysym_map[0x4b] = Key::KP_DIVIDE;
+ keysym_map[0x4c] = Key::KP_ENTER;
+ keysym_map[0x4e] = Key::KP_SUBTRACT;
+ keysym_map[0x4f] = Key::F18;
+ keysym_map[0x50] = Key::F19;
+ keysym_map[0x51] = Key::EQUAL;
+ keysym_map[0x52] = Key::KP_0;
+ keysym_map[0x53] = Key::KP_1;
+ keysym_map[0x54] = Key::KP_2;
+ keysym_map[0x55] = Key::KP_3;
+ keysym_map[0x56] = Key::KP_4;
+ keysym_map[0x57] = Key::KP_5;
+ keysym_map[0x58] = Key::KP_6;
+ keysym_map[0x59] = Key::KP_7;
+ keysym_map[0x5a] = Key::F20;
+ keysym_map[0x5b] = Key::KP_8;
+ keysym_map[0x5c] = Key::KP_9;
+ keysym_map[0x5d] = Key::YEN;
+ keysym_map[0x5e] = Key::UNDERSCORE;
+ keysym_map[0x5f] = Key::COMMA;
+ keysym_map[0x60] = Key::F5;
+ keysym_map[0x61] = Key::F6;
+ keysym_map[0x62] = Key::F7;
+ keysym_map[0x63] = Key::F3;
+ keysym_map[0x64] = Key::F8;
+ keysym_map[0x65] = Key::F9;
+ keysym_map[0x66] = Key::JIS_EISU;
+ keysym_map[0x67] = Key::F11;
+ keysym_map[0x68] = Key::JIS_KANA;
+ keysym_map[0x69] = Key::F13;
+ keysym_map[0x6a] = Key::F16;
+ keysym_map[0x6b] = Key::F14;
+ keysym_map[0x6d] = Key::F10;
+ keysym_map[0x6e] = Key::MENU;
+ keysym_map[0x6f] = Key::F12;
+ keysym_map[0x71] = Key::F15;
+ keysym_map[0x72] = Key::INSERT;
+ keysym_map[0x73] = Key::HOME;
+ keysym_map[0x74] = Key::PAGEUP;
+ keysym_map[0x75] = Key::KEY_DELETE;
+ keysym_map[0x76] = Key::F4;
+ keysym_map[0x77] = Key::END;
+ keysym_map[0x78] = Key::F2;
+ keysym_map[0x79] = Key::PAGEDOWN;
+ keysym_map[0x7a] = Key::F1;
+ keysym_map[0x7b] = Key::LEFT;
+ keysym_map[0x7c] = Key::RIGHT;
+ keysym_map[0x7d] = Key::DOWN;
+ keysym_map[0x7e] = Key::UP;
+
+ for (const KeyValue<unsigned int, Key> &E : keysym_map) {
+ keysym_map_inv[E.value] = E.key;
}
- return false;
+
+ keycode_map[Key::ESCAPE] = 0x001B;
+ keycode_map[Key::TAB] = 0x0009;
+ keycode_map[Key::BACKTAB] = 0x007F;
+ keycode_map[Key::BACKSPACE] = 0x0008;
+ keycode_map[Key::ENTER] = 0x000D;
+ keycode_map[Key::INSERT] = NSInsertFunctionKey;
+ keycode_map[Key::KEY_DELETE] = 0x007F;
+ keycode_map[Key::PAUSE] = NSPauseFunctionKey;
+ keycode_map[Key::PRINT] = NSPrintScreenFunctionKey;
+ keycode_map[Key::SYSREQ] = NSSysReqFunctionKey;
+ keycode_map[Key::CLEAR] = NSClearLineFunctionKey;
+ keycode_map[Key::HOME] = 0x2196;
+ keycode_map[Key::END] = 0x2198;
+ keycode_map[Key::LEFT] = 0x001C;
+ keycode_map[Key::UP] = 0x001E;
+ keycode_map[Key::RIGHT] = 0x001D;
+ keycode_map[Key::DOWN] = 0x001F;
+ keycode_map[Key::PAGEUP] = 0x21DE;
+ keycode_map[Key::PAGEDOWN] = 0x21DF;
+ keycode_map[Key::NUMLOCK] = NSClearLineFunctionKey;
+ keycode_map[Key::SCROLLLOCK] = NSScrollLockFunctionKey;
+ keycode_map[Key::F1] = NSF1FunctionKey;
+ keycode_map[Key::F2] = NSF2FunctionKey;
+ keycode_map[Key::F3] = NSF3FunctionKey;
+ keycode_map[Key::F4] = NSF4FunctionKey;
+ keycode_map[Key::F5] = NSF5FunctionKey;
+ keycode_map[Key::F6] = NSF6FunctionKey;
+ keycode_map[Key::F7] = NSF7FunctionKey;
+ keycode_map[Key::F8] = NSF8FunctionKey;
+ keycode_map[Key::F9] = NSF9FunctionKey;
+ keycode_map[Key::F10] = NSF10FunctionKey;
+ keycode_map[Key::F11] = NSF11FunctionKey;
+ keycode_map[Key::F12] = NSF12FunctionKey;
+ keycode_map[Key::F13] = NSF13FunctionKey;
+ keycode_map[Key::F14] = NSF14FunctionKey;
+ keycode_map[Key::F15] = NSF15FunctionKey;
+ keycode_map[Key::F16] = NSF16FunctionKey;
+ keycode_map[Key::F17] = NSF17FunctionKey;
+ keycode_map[Key::F18] = NSF18FunctionKey;
+ keycode_map[Key::F19] = NSF19FunctionKey;
+ keycode_map[Key::F20] = NSF20FunctionKey;
+ keycode_map[Key::F21] = NSF21FunctionKey;
+ keycode_map[Key::F22] = NSF22FunctionKey;
+ keycode_map[Key::F23] = NSF23FunctionKey;
+ keycode_map[Key::F24] = NSF24FunctionKey;
+ keycode_map[Key::F25] = NSF25FunctionKey;
+ keycode_map[Key::F26] = NSF26FunctionKey;
+ keycode_map[Key::F27] = NSF27FunctionKey;
+ keycode_map[Key::F28] = NSF28FunctionKey;
+ keycode_map[Key::F29] = NSF29FunctionKey;
+ keycode_map[Key::F30] = NSF30FunctionKey;
+ keycode_map[Key::F31] = NSF31FunctionKey;
+ keycode_map[Key::F32] = NSF32FunctionKey;
+ keycode_map[Key::F33] = NSF33FunctionKey;
+ keycode_map[Key::F34] = NSF34FunctionKey;
+ keycode_map[Key::F35] = NSF35FunctionKey;
+ keycode_map[Key::MENU] = NSMenuFunctionKey;
+ keycode_map[Key::HELP] = NSHelpFunctionKey;
+ keycode_map[Key::STOP] = NSStopFunctionKey;
+ keycode_map[Key::LAUNCH0] = NSUserFunctionKey;
+ keycode_map[Key::SPACE] = 0x0020;
+ keycode_map[Key::EXCLAM] = '!';
+ keycode_map[Key::QUOTEDBL] = '\"';
+ keycode_map[Key::NUMBERSIGN] = '#';
+ keycode_map[Key::DOLLAR] = '$';
+ keycode_map[Key::PERCENT] = '\%';
+ keycode_map[Key::AMPERSAND] = '&';
+ keycode_map[Key::APOSTROPHE] = '\'';
+ keycode_map[Key::PARENLEFT] = '(';
+ keycode_map[Key::PARENRIGHT] = ')';
+ keycode_map[Key::ASTERISK] = '*';
+ keycode_map[Key::PLUS] = '+';
+ keycode_map[Key::COMMA] = ',';
+ keycode_map[Key::MINUS] = '-';
+ keycode_map[Key::PERIOD] = '.';
+ keycode_map[Key::SLASH] = '/';
+ keycode_map[Key::KEY_0] = '0';
+ keycode_map[Key::KEY_1] = '1';
+ keycode_map[Key::KEY_2] = '2';
+ keycode_map[Key::KEY_3] = '3';
+ keycode_map[Key::KEY_4] = '4';
+ keycode_map[Key::KEY_5] = '5';
+ keycode_map[Key::KEY_6] = '6';
+ keycode_map[Key::KEY_7] = '7';
+ keycode_map[Key::KEY_8] = '8';
+ keycode_map[Key::KEY_9] = '9';
+ keycode_map[Key::COLON] = ':';
+ keycode_map[Key::SEMICOLON] = ';';
+ keycode_map[Key::LESS] = '<';
+ keycode_map[Key::EQUAL] = '=';
+ keycode_map[Key::GREATER] = '>';
+ keycode_map[Key::QUESTION] = '?';
+ keycode_map[Key::AT] = '@';
+ keycode_map[Key::A] = 'a';
+ keycode_map[Key::B] = 'b';
+ keycode_map[Key::C] = 'c';
+ keycode_map[Key::D] = 'd';
+ keycode_map[Key::E] = 'e';
+ keycode_map[Key::F] = 'f';
+ keycode_map[Key::G] = 'g';
+ keycode_map[Key::H] = 'h';
+ keycode_map[Key::I] = 'i';
+ keycode_map[Key::J] = 'j';
+ keycode_map[Key::K] = 'k';
+ keycode_map[Key::L] = 'l';
+ keycode_map[Key::M] = 'm';
+ keycode_map[Key::N] = 'n';
+ keycode_map[Key::O] = 'o';
+ keycode_map[Key::P] = 'p';
+ keycode_map[Key::Q] = 'q';
+ keycode_map[Key::R] = 'r';
+ keycode_map[Key::S] = 's';
+ keycode_map[Key::T] = 't';
+ keycode_map[Key::U] = 'u';
+ keycode_map[Key::V] = 'v';
+ keycode_map[Key::W] = 'w';
+ keycode_map[Key::X] = 'x';
+ keycode_map[Key::Y] = 'y';
+ keycode_map[Key::Z] = 'z';
+ keycode_map[Key::BRACKETLEFT] = '[';
+ keycode_map[Key::BACKSLASH] = '\\';
+ keycode_map[Key::BRACKETRIGHT] = ']';
+ keycode_map[Key::ASCIICIRCUM] = '^';
+ keycode_map[Key::UNDERSCORE] = '_';
+ keycode_map[Key::QUOTELEFT] = '`';
+ keycode_map[Key::BRACELEFT] = '{';
+ keycode_map[Key::BAR] = '|';
+ keycode_map[Key::BRACERIGHT] = '}';
+ keycode_map[Key::ASCIITILDE] = '~';
}
-// Keyboard symbol translation table.
-static const Key _macos_to_godot_table[128] = {
- /* 00 */ Key::A,
- /* 01 */ Key::S,
- /* 02 */ Key::D,
- /* 03 */ Key::F,
- /* 04 */ Key::H,
- /* 05 */ Key::G,
- /* 06 */ Key::Z,
- /* 07 */ Key::X,
- /* 08 */ Key::C,
- /* 09 */ Key::V,
- /* 0a */ Key::SECTION, /* ISO Section */
- /* 0b */ Key::B,
- /* 0c */ Key::Q,
- /* 0d */ Key::W,
- /* 0e */ Key::E,
- /* 0f */ Key::R,
- /* 10 */ Key::Y,
- /* 11 */ Key::T,
- /* 12 */ Key::KEY_1,
- /* 13 */ Key::KEY_2,
- /* 14 */ Key::KEY_3,
- /* 15 */ Key::KEY_4,
- /* 16 */ Key::KEY_6,
- /* 17 */ Key::KEY_5,
- /* 18 */ Key::EQUAL,
- /* 19 */ Key::KEY_9,
- /* 1a */ Key::KEY_7,
- /* 1b */ Key::MINUS,
- /* 1c */ Key::KEY_8,
- /* 1d */ Key::KEY_0,
- /* 1e */ Key::BRACERIGHT,
- /* 1f */ Key::O,
- /* 20 */ Key::U,
- /* 21 */ Key::BRACELEFT,
- /* 22 */ Key::I,
- /* 23 */ Key::P,
- /* 24 */ Key::ENTER,
- /* 25 */ Key::L,
- /* 26 */ Key::J,
- /* 27 */ Key::APOSTROPHE,
- /* 28 */ Key::K,
- /* 29 */ Key::SEMICOLON,
- /* 2a */ Key::BACKSLASH,
- /* 2b */ Key::COMMA,
- /* 2c */ Key::SLASH,
- /* 2d */ Key::N,
- /* 2e */ Key::M,
- /* 2f */ Key::PERIOD,
- /* 30 */ Key::TAB,
- /* 31 */ Key::SPACE,
- /* 32 */ Key::QUOTELEFT,
- /* 33 */ Key::BACKSPACE,
- /* 34 */ Key::UNKNOWN,
- /* 35 */ Key::ESCAPE,
- /* 36 */ Key::META,
- /* 37 */ Key::META,
- /* 38 */ Key::SHIFT,
- /* 39 */ Key::CAPSLOCK,
- /* 3a */ Key::ALT,
- /* 3b */ Key::CTRL,
- /* 3c */ Key::SHIFT,
- /* 3d */ Key::ALT,
- /* 3e */ Key::CTRL,
- /* 3f */ Key::UNKNOWN, /* Function */
- /* 40 */ Key::F17,
- /* 41 */ Key::KP_PERIOD,
- /* 42 */ Key::UNKNOWN,
- /* 43 */ Key::KP_MULTIPLY,
- /* 44 */ Key::UNKNOWN,
- /* 45 */ Key::KP_ADD,
- /* 46 */ Key::UNKNOWN,
- /* 47 */ Key::NUMLOCK, /* Really KeypadClear... */
- /* 48 */ Key::VOLUMEUP, /* VolumeUp */
- /* 49 */ Key::VOLUMEDOWN, /* VolumeDown */
- /* 4a */ Key::VOLUMEMUTE, /* Mute */
- /* 4b */ Key::KP_DIVIDE,
- /* 4c */ Key::KP_ENTER,
- /* 4d */ Key::UNKNOWN,
- /* 4e */ Key::KP_SUBTRACT,
- /* 4f */ Key::F18,
- /* 50 */ Key::F19,
- /* 51 */ Key::EQUAL, /* KeypadEqual */
- /* 52 */ Key::KP_0,
- /* 53 */ Key::KP_1,
- /* 54 */ Key::KP_2,
- /* 55 */ Key::KP_3,
- /* 56 */ Key::KP_4,
- /* 57 */ Key::KP_5,
- /* 58 */ Key::KP_6,
- /* 59 */ Key::KP_7,
- /* 5a */ Key::F20,
- /* 5b */ Key::KP_8,
- /* 5c */ Key::KP_9,
- /* 5d */ Key::YEN, /* JIS Yen */
- /* 5e */ Key::UNDERSCORE, /* JIS Underscore */
- /* 5f */ Key::COMMA, /* JIS KeypadComma */
- /* 60 */ Key::F5,
- /* 61 */ Key::F6,
- /* 62 */ Key::F7,
- /* 63 */ Key::F3,
- /* 64 */ Key::F8,
- /* 65 */ Key::F9,
- /* 66 */ Key::UNKNOWN, /* JIS Eisu */
- /* 67 */ Key::F11,
- /* 68 */ Key::UNKNOWN, /* JIS Kana */
- /* 69 */ Key::F13,
- /* 6a */ Key::F16,
- /* 6b */ Key::F14,
- /* 6c */ Key::UNKNOWN,
- /* 6d */ Key::F10,
- /* 6e */ Key::MENU,
- /* 6f */ Key::F12,
- /* 70 */ Key::UNKNOWN,
- /* 71 */ Key::F15,
- /* 72 */ Key::INSERT, /* Really Help... */
- /* 73 */ Key::HOME,
- /* 74 */ Key::PAGEUP,
- /* 75 */ Key::KEY_DELETE,
- /* 76 */ Key::F4,
- /* 77 */ Key::END,
- /* 78 */ Key::F2,
- /* 79 */ Key::PAGEDOWN,
- /* 7a */ Key::F1,
- /* 7b */ Key::LEFT,
- /* 7c */ Key::RIGHT,
- /* 7d */ Key::DOWN,
- /* 7e */ Key::UP,
- /* 7f */ Key::UNKNOWN,
-};
+bool KeyMappingMacOS::is_numpad_key(unsigned int p_key) {
+ return numpad_keys.has(p_key);
+}
// Translates a OS X keycode to a Godot keycode.
-Key KeyMappingMacOS::translate_key(unsigned int key) {
- if (key >= 128) {
- return Key::UNKNOWN;
+Key KeyMappingMacOS::translate_key(unsigned int p_key) {
+ const Key *key = keysym_map.getptr(p_key);
+ if (key) {
+ return *key;
}
-
- return _macos_to_godot_table[key];
+ return Key::NONE;
}
// Translates a Godot keycode back to a macOS keycode.
-unsigned int KeyMappingMacOS::unmap_key(Key key) {
- for (int i = 0; i <= 126; i++) {
- if (_macos_to_godot_table[i] == key) {
- return i;
- }
+unsigned int KeyMappingMacOS::unmap_key(Key p_key) {
+ const unsigned int *key = keysym_map_inv.getptr(p_key);
+ if (key) {
+ return *key;
}
return 127;
}
-struct _KeyCodeMap {
- UniChar kchar;
- Key kcode;
-};
-
-static const _KeyCodeMap _keycodes[55] = {
- { '`', Key::QUOTELEFT },
- { '~', Key::ASCIITILDE },
- { '0', Key::KEY_0 },
- { '1', Key::KEY_1 },
- { '2', Key::KEY_2 },
- { '3', Key::KEY_3 },
- { '4', Key::KEY_4 },
- { '5', Key::KEY_5 },
- { '6', Key::KEY_6 },
- { '7', Key::KEY_7 },
- { '8', Key::KEY_8 },
- { '9', Key::KEY_9 },
- { '-', Key::MINUS },
- { '_', Key::UNDERSCORE },
- { '=', Key::EQUAL },
- { '+', Key::PLUS },
- { 'q', Key::Q },
- { 'w', Key::W },
- { 'e', Key::E },
- { 'r', Key::R },
- { 't', Key::T },
- { 'y', Key::Y },
- { 'u', Key::U },
- { 'i', Key::I },
- { 'o', Key::O },
- { 'p', Key::P },
- { '[', Key::BRACELEFT },
- { ']', Key::BRACERIGHT },
- { '{', Key::BRACELEFT },
- { '}', Key::BRACERIGHT },
- { 'a', Key::A },
- { 's', Key::S },
- { 'd', Key::D },
- { 'f', Key::F },
- { 'g', Key::G },
- { 'h', Key::H },
- { 'j', Key::J },
- { 'k', Key::K },
- { 'l', Key::L },
- { ';', Key::SEMICOLON },
- { ':', Key::COLON },
- { '\'', Key::APOSTROPHE },
- { '\"', Key::QUOTEDBL },
- { '\\', Key::BACKSLASH },
- { '#', Key::NUMBERSIGN },
- { 'z', Key::Z },
- { 'x', Key::X },
- { 'c', Key::C },
- { 'v', Key::V },
- { 'b', Key::B },
- { 'n', Key::N },
- { 'm', Key::M },
- { ',', Key::COMMA },
- { '.', Key::PERIOD },
- { '/', Key::SLASH }
-};
-
// Remap key according to current keyboard layout.
-Key KeyMappingMacOS::remap_key(unsigned int key, unsigned int state) {
- if (is_numpad_key(key)) {
- return translate_key(key);
+Key KeyMappingMacOS::remap_key(unsigned int p_key, unsigned int p_state, bool p_unicode) {
+ if (is_numpad_key(p_key)) {
+ return translate_key(p_key);
}
TISInputSourceRef current_keyboard = TISCopyCurrentKeyboardInputSource();
if (!current_keyboard) {
- return translate_key(key);
+ return translate_key(p_key);
}
CFDataRef layout_data = (CFDataRef)TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData);
if (!layout_data) {
- return translate_key(key);
+ return translate_key(p_key);
}
const UCKeyboardLayout *keyboard_layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);
+ String keysym;
UInt32 keys_down = 0;
- UniChar chars[4];
- UniCharCount real_length;
+ UniChar chars[256] = {};
+ UniCharCount real_length = 0;
OSStatus err = UCKeyTranslate(keyboard_layout,
- key,
+ p_key,
kUCKeyActionDisplay,
- (state >> 8) & 0xFF,
+ (p_unicode) ? 0 : (p_state >> 8) & 0xFF,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&keys_down,
@@ -312,165 +380,26 @@ Key KeyMappingMacOS::remap_key(unsigned int key, unsigned int state) {
chars);
if (err != noErr) {
- return translate_key(key);
+ return translate_key(p_key);
}
- for (unsigned int i = 0; i < 55; i++) {
- if (_keycodes[i].kchar == chars[0]) {
- return _keycodes[i].kcode;
- }
+ keysym = String::utf16((char16_t *)chars, real_length);
+ if (keysym.is_empty()) {
+ return translate_key(p_key);
}
- return translate_key(key);
-}
-
-struct _KeyCodeText {
- Key code;
- char32_t text;
-};
-static const _KeyCodeText _native_keycodes[] = {
- /* clang-format off */
- {Key::ESCAPE ,0x001B},
- {Key::TAB ,0x0009},
- {Key::BACKTAB ,0x007F},
- {Key::BACKSPACE ,0x0008},
- {Key::ENTER ,0x000D},
- {Key::INSERT ,NSInsertFunctionKey},
- {Key::KEY_DELETE ,0x007F},
- {Key::PAUSE ,NSPauseFunctionKey},
- {Key::PRINT ,NSPrintScreenFunctionKey},
- {Key::SYSREQ ,NSSysReqFunctionKey},
- {Key::CLEAR ,NSClearLineFunctionKey},
- {Key::HOME ,0x2196},
- {Key::END ,0x2198},
- {Key::LEFT ,0x001C},
- {Key::UP ,0x001E},
- {Key::RIGHT ,0x001D},
- {Key::DOWN ,0x001F},
- {Key::PAGEUP ,0x21DE},
- {Key::PAGEDOWN ,0x21DF},
- {Key::NUMLOCK ,NSClearLineFunctionKey},
- {Key::SCROLLLOCK ,NSScrollLockFunctionKey},
- {Key::F1 ,NSF1FunctionKey},
- {Key::F2 ,NSF2FunctionKey},
- {Key::F3 ,NSF3FunctionKey},
- {Key::F4 ,NSF4FunctionKey},
- {Key::F5 ,NSF5FunctionKey},
- {Key::F6 ,NSF6FunctionKey},
- {Key::F7 ,NSF7FunctionKey},
- {Key::F8 ,NSF8FunctionKey},
- {Key::F9 ,NSF9FunctionKey},
- {Key::F10 ,NSF10FunctionKey},
- {Key::F11 ,NSF11FunctionKey},
- {Key::F12 ,NSF12FunctionKey},
- {Key::F13 ,NSF13FunctionKey},
- {Key::F14 ,NSF14FunctionKey},
- {Key::F15 ,NSF15FunctionKey},
- {Key::F16 ,NSF16FunctionKey},
- {Key::F17 ,NSF17FunctionKey},
- {Key::F18 ,NSF18FunctionKey},
- {Key::F19 ,NSF19FunctionKey},
- {Key::F20 ,NSF20FunctionKey},
- {Key::F21 ,NSF21FunctionKey},
- {Key::F22 ,NSF22FunctionKey},
- {Key::F23 ,NSF23FunctionKey},
- {Key::F24 ,NSF24FunctionKey},
- {Key::F25 ,NSF25FunctionKey},
- {Key::F26 ,NSF26FunctionKey},
- {Key::F27 ,NSF27FunctionKey},
- {Key::F28 ,NSF28FunctionKey},
- {Key::F29 ,NSF29FunctionKey},
- {Key::F30 ,NSF30FunctionKey},
- {Key::F31 ,NSF31FunctionKey},
- {Key::F32 ,NSF32FunctionKey},
- {Key::F33 ,NSF33FunctionKey},
- {Key::F34 ,NSF34FunctionKey},
- {Key::F35 ,NSF35FunctionKey},
- {Key::MENU ,NSMenuFunctionKey},
- {Key::HELP ,NSHelpFunctionKey},
- {Key::STOP ,NSStopFunctionKey},
- {Key::LAUNCH0 ,NSUserFunctionKey},
- {Key::SPACE ,0x0020},
- {Key::EXCLAM ,'!'},
- {Key::QUOTEDBL ,'\"'},
- {Key::NUMBERSIGN ,'#'},
- {Key::DOLLAR ,'$'},
- {Key::PERCENT ,'\%'},
- {Key::AMPERSAND ,'&'},
- {Key::APOSTROPHE ,'\''},
- {Key::PARENLEFT ,'('},
- {Key::PARENRIGHT ,')'},
- {Key::ASTERISK ,'*'},
- {Key::PLUS ,'+'},
- {Key::COMMA ,','},
- {Key::MINUS ,'-'},
- {Key::PERIOD ,'.'},
- {Key::SLASH ,'/'},
- {Key::KEY_0 ,'0'},
- {Key::KEY_1 ,'1'},
- {Key::KEY_2 ,'2'},
- {Key::KEY_3 ,'3'},
- {Key::KEY_4 ,'4'},
- {Key::KEY_5 ,'5'},
- {Key::KEY_6 ,'6'},
- {Key::KEY_7 ,'7'},
- {Key::KEY_8 ,'8'},
- {Key::KEY_9 ,'9'},
- {Key::COLON ,':'},
- {Key::SEMICOLON ,';'},
- {Key::LESS ,'<'},
- {Key::EQUAL ,'='},
- {Key::GREATER ,'>'},
- {Key::QUESTION ,'?'},
- {Key::AT ,'@'},
- {Key::A ,'a'},
- {Key::B ,'b'},
- {Key::C ,'c'},
- {Key::D ,'d'},
- {Key::E ,'e'},
- {Key::F ,'f'},
- {Key::G ,'g'},
- {Key::H ,'h'},
- {Key::I ,'i'},
- {Key::J ,'j'},
- {Key::K ,'k'},
- {Key::L ,'l'},
- {Key::M ,'m'},
- {Key::N ,'n'},
- {Key::O ,'o'},
- {Key::P ,'p'},
- {Key::Q ,'q'},
- {Key::R ,'r'},
- {Key::S ,'s'},
- {Key::T ,'t'},
- {Key::U ,'u'},
- {Key::V ,'v'},
- {Key::W ,'w'},
- {Key::X ,'x'},
- {Key::Y ,'y'},
- {Key::Z ,'z'},
- {Key::BRACKETLEFT ,'['},
- {Key::BACKSLASH ,'\\'},
- {Key::BRACKETRIGHT ,']'},
- {Key::ASCIICIRCUM ,'^'},
- {Key::UNDERSCORE ,'_'},
- {Key::QUOTELEFT ,'`'},
- {Key::BRACELEFT ,'{'},
- {Key::BAR ,'|'},
- {Key::BRACERIGHT ,'}'},
- {Key::ASCIITILDE ,'~'},
- {Key::NONE ,0x0000}
- /* clang-format on */
-};
+ char32_t c = keysym[0];
+ if (p_unicode) {
+ return fix_key_label(c, translate_key(p_key));
+ } else {
+ return fix_keycode(c, translate_key(p_key));
+ }
+}
String KeyMappingMacOS::keycode_get_native_string(Key p_keycode) {
- const _KeyCodeText *kct = &_native_keycodes[0];
-
- while (kct->text) {
- if (kct->code == p_keycode) {
- return String::chr(kct->text);
- }
- kct++;
+ const char32_t *key = keycode_map.getptr(p_keycode);
+ if (key) {
+ return String::chr(*key);
}
return String();
}