diff --git a/src/utility/Keyboard/Keyboard.cpp b/src/utility/Keyboard/Keyboard.cpp index 968cd6b..da602a5 100644 --- a/src/utility/Keyboard/Keyboard.cpp +++ b/src/utility/Keyboard/Keyboard.cpp @@ -118,6 +118,8 @@ void Keyboard_Class::updateKeysState() switch (key_code) { case KEY_FN: _keys_state_buffer.fn = true; + _key_pos_modifier_keys.push_back(key_pos); + _keys_state_buffer.modifier_keys.push_back(key_code); break; case KEY_OPT: _keys_state_buffer.opt = true; @@ -145,7 +147,85 @@ void Keyboard_Class::updateKeysState() } // ================================================================= - // PASS 2: Process all non-modifier keys. + // PASS 2: Process fn-layer keys. + // Now the modifier state is correct, regardless of key order. + // The fn-layer has precedence over special keys too + // ================================================================= + if (_keys_state_buffer.fn) { + for (const auto& key_pos : keys) { + const KeyValue_t key_value = getKeyValue(key_pos); + const uint8_t key_code = key_value.value_third; + + // printf("%d\n", key_code); + if (key_code == KEY_NONE) { + continue; + } + + // Skip modifier keys as they were handled in the first pass + switch (key_code) { + case KEY_ESCAPE: + _keys_state_buffer.esc = true; + break; + case KEY_DELETE: + _keys_state_buffer.del = true; + break; + case KEY_F1: + _keys_state_buffer.f1 = true; + break; + case KEY_F2: + _keys_state_buffer.f2 = true; + break; + case KEY_F3: + _keys_state_buffer.f3 = true; + break; + case KEY_F4: + _keys_state_buffer.f4 = true; + break; + case KEY_F5: + _keys_state_buffer.f5 = true; + break; + case KEY_F6: + _keys_state_buffer.f6 = true; + break; + case KEY_F7: + _keys_state_buffer.f7 = true; + break; + case KEY_F8: + _keys_state_buffer.f8 = true; + break; + case KEY_F9: + _keys_state_buffer.f9 = true; + break; + case KEY_F10: + _keys_state_buffer.f10 = true; + break; + case KEY_F11: + _keys_state_buffer.f11 = true; + break; + case KEY_F12: + _keys_state_buffer.f12 = true; + break; + case KEY_UP: + _keys_state_buffer.up = true; + break; + case KEY_LEFT: + _keys_state_buffer.left = true; + break; + case KEY_DOWN: + _keys_state_buffer.down = true; + break; + case KEY_RIGHT: + _keys_state_buffer.right = true; + break; + } + _key_pos_hid_keys.push_back(key_pos); + _keys_state_buffer.hid_keys.push_back(key_code); + } + return; + } + + // ================================================================= + // PASS 3: Process all non-modifier keys. // Now the modifier state is correct, regardless of key order. // ================================================================= for (const auto& key_pos : keys) { @@ -156,7 +236,6 @@ void Keyboard_Class::updateKeysState() // Skip modifier keys as they were handled in the first pass switch (key_code) { - case KEY_FN: case KEY_OPT: case KEY_LEFT_CTRL: case KEY_LEFT_SHIFT: @@ -172,7 +251,7 @@ void Keyboard_Class::updateKeysState() _keys_state_buffer.hid_keys.push_back(key_code); continue; // Skip further processing for this key case KEY_BACKSPACE: - _keys_state_buffer.del = true; + _keys_state_buffer.backspace = true; _key_pos_hid_keys.push_back(key_pos); _keys_state_buffer.hid_keys.push_back(key_code); continue; // Skip further processing for this key diff --git a/src/utility/Keyboard/Keyboard.h b/src/utility/Keyboard/Keyboard.h index 14018d3..93b2761 100644 --- a/src/utility/Keyboard/Keyboard.h +++ b/src/utility/Keyboard/Keyboard.h @@ -12,65 +12,66 @@ struct KeyValue_t { const char value_first; - const char value_second; + const char value_second; // Aa layer + const char value_third; // fn layer }; -const KeyValue_t _key_value_map[4][14] = {{{'`', '~'}, - {'1', '!'}, - {'2', '@'}, - {'3', '#'}, - {'4', '$'}, - {'5', '%'}, - {'6', '^'}, - {'7', '&'}, - {'8', '*'}, - {'9', '('}, - {'0', ')'}, - {'-', '_'}, - {'=', '+'}, - {KEY_BACKSPACE, KEY_BACKSPACE}}, - {{KEY_TAB, KEY_TAB}, - {'q', 'Q'}, - {'w', 'W'}, - {'e', 'E'}, - {'r', 'R'}, - {'t', 'T'}, - {'y', 'Y'}, - {'u', 'U'}, - {'i', 'I'}, - {'o', 'O'}, - {'p', 'P'}, - {'[', '{'}, - {']', '}'}, - {'\\', '|'}}, - {{KEY_FN, KEY_FN}, - {KEY_LEFT_SHIFT, KEY_LEFT_SHIFT}, - {'a', 'A'}, - {'s', 'S'}, - {'d', 'D'}, - {'f', 'F'}, - {'g', 'G'}, - {'h', 'H'}, - {'j', 'J'}, - {'k', 'K'}, - {'l', 'L'}, - {';', ':'}, - {'\'', '\"'}, - {KEY_ENTER, KEY_ENTER}}, - {{KEY_LEFT_CTRL, KEY_LEFT_CTRL}, - {KEY_OPT, KEY_OPT}, - {KEY_LEFT_ALT, KEY_LEFT_ALT}, - {'z', 'Z'}, - {'x', 'X'}, - {'c', 'C'}, - {'v', 'V'}, - {'b', 'B'}, - {'n', 'N'}, - {'m', 'M'}, - {',', '<'}, - {'.', '>'}, - {'/', '?'}, - {' ', ' '}}}; +const KeyValue_t _key_value_map[4][14] = {{{'`', '~', KEY_ESCAPE}, + {'1', '!', KEY_F1}, + {'2', '@', KEY_F2}, + {'3', '#', KEY_F3}, + {'4', '$', KEY_F4}, + {'5', '%', KEY_F5}, + {'6', '^', KEY_F6}, + {'7', '&', KEY_F7}, + {'8', '*', KEY_F8}, + {'9', '(', KEY_F9}, + {'0', ')', KEY_F10}, + {'-', '_', KEY_F11}, + {'=', '+', KEY_F12}, + {KEY_BACKSPACE, KEY_BACKSPACE, KEY_DELETE}}, + {{KEY_TAB, KEY_TAB, KEY_NONE}, + {'q', 'Q', KEY_NONE}, + {'w', 'W', KEY_NONE}, + {'e', 'E', KEY_NONE}, + {'r', 'R', KEY_NONE}, + {'t', 'T', KEY_NONE}, + {'y', 'Y', KEY_NONE}, + {'u', 'U', KEY_NONE}, + {'i', 'I', KEY_NONE}, + {'o', 'O', KEY_NONE}, + {'p', 'P', KEY_NONE}, + {'[', '{', KEY_NONE}, + {']', '}', KEY_NONE}, + {'\\', '|', KEY_NONE}}, + {{KEY_FN, KEY_FN, KEY_FN}, + {KEY_LEFT_SHIFT, KEY_LEFT_SHIFT, KEY_NONE}, + {'a', 'A', KEY_NONE}, + {'s', 'S', KEY_NONE}, + {'d', 'D', KEY_NONE}, + {'f', 'F', KEY_NONE}, + {'g', 'G', KEY_NONE}, + {'h', 'H', KEY_NONE}, + {'j', 'J', KEY_NONE}, + {'k', 'K', KEY_NONE}, + {'l', 'L', KEY_NONE}, + {';', ':', KEY_UP}, + {'\'', '\"', KEY_NONE}, + {KEY_ENTER, KEY_ENTER, KEY_NONE}}, + {{KEY_LEFT_CTRL, KEY_LEFT_CTRL, KEY_NONE}, + {KEY_OPT, KEY_OPT, KEY_NONE}, + {KEY_LEFT_ALT, KEY_LEFT_ALT, KEY_NONE}, + {'z', 'Z', KEY_NONE}, + {'x', 'X', KEY_NONE}, + {'c', 'C', KEY_NONE}, + {'v', 'V', KEY_NONE}, + {'b', 'B', KEY_NONE}, + {'n', 'N', KEY_NONE}, + {'m', 'M', KEY_NONE}, + {',', '<', KEY_LEFT}, + {'.', '>', KEY_DOWN}, + {'/', '?', KEY_RIGHT}, + {' ', ' ', KEY_NONE}}}; class Keyboard_Class { public: @@ -81,9 +82,27 @@ class Keyboard_Class { bool ctrl = false; bool opt = false; bool alt = false; + bool backspace = false; bool del = false; bool enter = false; bool space = false; + bool esc = false; + bool f1 = false; + bool f2 = false; + bool f3 = false; + bool f4 = false; + bool f5 = false; + bool f6 = false; + bool f7 = false; + bool f8 = false; + bool f9 = false; + bool f10 = false; + bool f11 = false; + bool f12 = false; + bool up = false; + bool left = false; + bool down = false; + bool right = false; uint8_t modifiers = 0; std::vector word; @@ -98,9 +117,27 @@ class Keyboard_Class { ctrl = false; opt = false; alt = false; + backspace = false; del = false; enter = false; space = false; + esc = false; + f1 = false; + f2 = false; + f3 = false; + f4 = false; + f5 = false; + f6 = false; + f7 = false; + f8 = false; + f9 = false; + f10 = false; + f11 = false; + f12 = false; + up = false; + left = false; + down = false; + right = false; modifiers = 0; word.clear(); hid_keys.clear(); diff --git a/src/utility/Keyboard/Keyboard_def.h b/src/utility/Keyboard/Keyboard_def.h index 823acc8..dd40f4b 100644 --- a/src/utility/Keyboard/Keyboard_def.h +++ b/src/utility/Keyboard/Keyboard_def.h @@ -17,9 +17,30 @@ #define KEY_FN 0xff #define KEY_OPT 0x00 +#define KEY_F1 0x3A +#define KEY_F2 0x3B +#define KEY_F3 0x3C +#define KEY_F4 0x3D +#define KEY_F5 0x3E +#define KEY_F6 0x3F +#define KEY_F7 0x40 +#define KEY_F8 0x41 +#define KEY_F9 0x42 +#define KEY_F10 0x43 +#define KEY_F11 0x44 +#define KEY_F12 0x45 + +#define KEY_RIGHT 0x4F +#define KEY_LEFT 0x50 +#define KEY_DOWN 0x51 +#define KEY_UP 0x52 + #define KEY_BACKSPACE 0x2a #define KEY_TAB 0x2b #define KEY_ENTER 0x28 +#define KEY_ESCAPE 0x29 +#define KEY_DELETE 0x4c +#define KEY_NONE 0x00 const uint8_t _kb_asciimap[128] = { 0x00, // NUL @@ -38,7 +59,7 @@ const uint8_t _kb_asciimap[128] = { 0x00, // CR 0x00, // SO 0x00, // SI - 0x00, // DEL + KEY_DELETE, // DEL 0x00, // DC1 0x00, // DC2 0x00, // DC3 @@ -49,7 +70,7 @@ const uint8_t _kb_asciimap[128] = { 0x00, // CAN 0x00, // EM 0x00, // SUB - 0x00, // ESC + KEY_ESCAPE, // ESC 0x00, // FS 0x00, // GS 0x00, // RS