|
| 1 | +#ifndef _KEYBOARD_LAYOUT_H_ |
| 2 | +#define _KEYBOARD_LAYOUT_H_ |
| 3 | + |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +// Keyboard layout configuration |
| 7 | +#define SPACE_ROW 7 |
| 8 | +#define KEYBOARD_ROWS (SPACE_ROW + 1) |
| 9 | + |
| 10 | +#define SPACE_START 0 |
| 11 | +#define SPACE_END 3 |
| 12 | +#define BACK_START 4 |
| 13 | +#define BACK_END 6 |
| 14 | +#define DONE_START 8 |
| 15 | +#define DONE_END 10 |
| 16 | + |
| 17 | +static const char* keyboardLayout[] = { |
| 18 | + "1234567890", |
| 19 | + "QWERTYUIOP", |
| 20 | + "ASDFGHJKL@", |
| 21 | + "ZXCVBNM,?>", |
| 22 | + "qwertyuiop", |
| 23 | + "asdfghjkl|", |
| 24 | + "zxcvbnm-_<", |
| 25 | + "[_] <- OK" |
| 26 | +}; |
| 27 | + |
| 28 | +// Helper functions for special row section detection |
| 29 | +inline bool isInSpaceSection(int col) { return col < SPACE_END; } |
| 30 | +inline bool isInBackSection(int col) { return col >= BACK_START && col < BACK_END; } |
| 31 | +inline bool isInDoneSection(int col) { return col >= DONE_START; } |
| 32 | + |
| 33 | +// Get the character at a specific keyboard position |
| 34 | +inline char getKeyAtPosition(int row, int col) { |
| 35 | + if (row < 0 || row >= KEYBOARD_ROWS) return '\0'; |
| 36 | + const char* rowStr = keyboardLayout[row]; |
| 37 | + |
| 38 | + // Handle special row with SPC, BACK, and DONE |
| 39 | + if (row == SPACE_ROW) { |
| 40 | + if (col >= 0 && col < SPACE_END) |
| 41 | + return ' '; // [_] (space) |
| 42 | + if (col >= BACK_START && col < BACK_END) return '\b'; // <- (backspace) |
| 43 | + if (col >= DONE_START && col < DONE_END) return '\r'; // OK (carriage return) |
| 44 | + return '\0'; |
| 45 | + } |
| 46 | + |
| 47 | + int len = strlen(rowStr); |
| 48 | + if (col < 0 || col >= len) return '\0'; |
| 49 | + return rowStr[col]; |
| 50 | +} |
| 51 | + |
| 52 | +// Find a character's position in the keyboard layout |
| 53 | +inline void findCharacterInKeyboard(char ch, int &outRow, int &outCol) { |
| 54 | + if (ch == ' ') return; // Skip space character |
| 55 | + |
| 56 | + // Search for character in keyboard layout (excluding special row) |
| 57 | + for (int row = 0; row < SPACE_ROW; row++) { |
| 58 | + const char* rowStr = keyboardLayout[row]; |
| 59 | + int len = strlen(rowStr); |
| 60 | + for (int col = 0; col < len; col++) { |
| 61 | + if (rowStr[col] == ch) { |
| 62 | + outRow = row; |
| 63 | + outCol = col; |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + // Character not found, don't change position |
| 69 | +} |
| 70 | + |
| 71 | +// Clamp keyboard column to valid range for current row |
| 72 | +inline void clampKeyboardColumn(int row, int& col) { |
| 73 | + if (row == SPACE_ROW) { |
| 74 | + if (col < SPACE_END) col = SPACE_START; |
| 75 | + else if (col <= BACK_END) col = BACK_START; |
| 76 | + else col = DONE_START; |
| 77 | + } else { |
| 78 | + int maxCol = strlen(keyboardLayout[row]) - 1; |
| 79 | + if (col > maxCol) col = 0; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Cycle keyboard column left (-1) or right (+1) within current row |
| 84 | +inline void cycleKeyboardColumn(int row, int direction, int& col) { |
| 85 | + if (row == SPACE_ROW) { |
| 86 | + if (direction < 0) { // LEFT |
| 87 | + if (isInSpaceSection(col)) |
| 88 | + col = DONE_START; |
| 89 | + else if (isInBackSection(col)) col = SPACE_START; |
| 90 | + else col = BACK_START; |
| 91 | + } else { // RIGHT |
| 92 | + if (isInBackSection(col)) col = DONE_START; |
| 93 | + else if (isInDoneSection(col)) col = SPACE_START; |
| 94 | + else col = BACK_START; |
| 95 | + } |
| 96 | + } else { |
| 97 | + int maxCol = strlen(keyboardLayout[row]) - 1; |
| 98 | + col = (col + direction + maxCol + 1) % (maxCol + 1); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#endif |
0 commit comments