|
| 1 | +package io.github.rickybrent.minimal_symlayer_keyboard |
| 2 | + |
| 3 | +import android.view.KeyEvent |
| 4 | + |
| 5 | +/** |
| 6 | + * Provides mappings from Latin QWERTY keys to Cyrillic characters. |
| 7 | + * Maps both lowercase and uppercase variants. |
| 8 | + */ |
| 9 | +object CyrillicMappings { |
| 10 | + |
| 11 | + /** |
| 12 | + * Mapping from Latin key codes to Cyrillic characters (lowercase) |
| 13 | + * Classic PC (JCUKEN-like) layout adapted to available keys. |
| 14 | + */ |
| 15 | + private val lowercaseMap: Map<Int, Char> = mapOf( |
| 16 | + // Top row |
| 17 | + KeyEvent.KEYCODE_Q to 'й', |
| 18 | + KeyEvent.KEYCODE_W to 'ц', |
| 19 | + KeyEvent.KEYCODE_E to 'у', |
| 20 | + KeyEvent.KEYCODE_R to 'к', |
| 21 | + KeyEvent.KEYCODE_T to 'е', |
| 22 | + KeyEvent.KEYCODE_Y to 'н', |
| 23 | + KeyEvent.KEYCODE_U to 'г', |
| 24 | + KeyEvent.KEYCODE_I to 'ш', |
| 25 | + KeyEvent.KEYCODE_O to 'щ', |
| 26 | + KeyEvent.KEYCODE_P to 'з', |
| 27 | + // Removed direct mappings for х and ъ (moved to Alt-combos) |
| 28 | + // KeyEvent.KEYCODE_LEFT_BRACKET to 'х', |
| 29 | + // KeyEvent.KEYCODE_RIGHT_BRACKET to 'ъ', |
| 30 | + // Removed MINUS to 'х' fallback per request |
| 31 | + // Removed EQUALS to 'э' fallback per request |
| 32 | + |
| 33 | + // Home row |
| 34 | + KeyEvent.KEYCODE_A to 'ф', |
| 35 | + KeyEvent.KEYCODE_S to 'ы', |
| 36 | + KeyEvent.KEYCODE_D to 'в', |
| 37 | + KeyEvent.KEYCODE_F to 'а', |
| 38 | + KeyEvent.KEYCODE_G to 'п', |
| 39 | + KeyEvent.KEYCODE_H to 'р', |
| 40 | + KeyEvent.KEYCODE_J to 'о', |
| 41 | + KeyEvent.KEYCODE_K to 'л', |
| 42 | + KeyEvent.KEYCODE_L to 'д', |
| 43 | + // Removed direct mappings for ж and э (moved to Alt-combos) |
| 44 | + // KeyEvent.KEYCODE_SEMICOLON to 'ж', |
| 45 | + // KeyEvent.KEYCODE_APOSTROPHE to 'э', |
| 46 | + |
| 47 | + // Bottom row |
| 48 | + KeyEvent.KEYCODE_Z to 'я', |
| 49 | + KeyEvent.KEYCODE_X to 'ч', |
| 50 | + KeyEvent.KEYCODE_C to 'с', |
| 51 | + KeyEvent.KEYCODE_V to 'м', |
| 52 | + KeyEvent.KEYCODE_B to 'и', |
| 53 | + KeyEvent.KEYCODE_N to 'т', |
| 54 | + KeyEvent.KEYCODE_M to 'ь', |
| 55 | + // Removed direct mappings for б and ю (moved to Alt-combos) |
| 56 | + // KeyEvent.KEYCODE_COMMA to 'б', |
| 57 | + // KeyEvent.KEYCODE_PERIOD to 'ю', |
| 58 | + KeyEvent.KEYCODE_SLASH to '.', |
| 59 | + KeyEvent.KEYCODE_BACKSLASH to '/', |
| 60 | + |
| 61 | + // Removed direct mapping for ё (moved to Alt-combo on K) |
| 62 | + // KeyEvent.KEYCODE_GRAVE to 'ё' |
| 63 | + ) |
| 64 | + |
| 65 | + /** |
| 66 | + * Mapping from Latin key codes to Cyrillic characters (uppercase) |
| 67 | + */ |
| 68 | + private val uppercaseMap: Map<Int, Char> = mapOf( |
| 69 | + // Top row |
| 70 | + KeyEvent.KEYCODE_Q to 'Й', |
| 71 | + KeyEvent.KEYCODE_W to 'Ц', |
| 72 | + KeyEvent.KEYCODE_E to 'У', |
| 73 | + KeyEvent.KEYCODE_R to 'К', |
| 74 | + KeyEvent.KEYCODE_T to 'Е', |
| 75 | + KeyEvent.KEYCODE_Y to 'Н', |
| 76 | + KeyEvent.KEYCODE_U to 'Г', |
| 77 | + KeyEvent.KEYCODE_I to 'Ш', |
| 78 | + KeyEvent.KEYCODE_O to 'Щ', |
| 79 | + KeyEvent.KEYCODE_P to 'З', |
| 80 | + // Removed direct mappings for Х and Ъ |
| 81 | + // Removed MINUS/EQUALS fallbacks |
| 82 | + |
| 83 | + // Home row |
| 84 | + KeyEvent.KEYCODE_A to 'Ф', |
| 85 | + KeyEvent.KEYCODE_S to 'Ы', |
| 86 | + KeyEvent.KEYCODE_D to 'В', |
| 87 | + KeyEvent.KEYCODE_F to 'А', |
| 88 | + KeyEvent.KEYCODE_G to 'П', |
| 89 | + KeyEvent.KEYCODE_H to 'Р', |
| 90 | + KeyEvent.KEYCODE_J to 'О', |
| 91 | + KeyEvent.KEYCODE_K to 'Л', |
| 92 | + KeyEvent.KEYCODE_L to 'Д', |
| 93 | + // Removed direct mappings for Ж and Э |
| 94 | + |
| 95 | + // Bottom row |
| 96 | + KeyEvent.KEYCODE_Z to 'Я', |
| 97 | + KeyEvent.KEYCODE_X to 'Ч', |
| 98 | + KeyEvent.KEYCODE_C to 'С', |
| 99 | + KeyEvent.KEYCODE_V to 'М', |
| 100 | + KeyEvent.KEYCODE_B to 'И', |
| 101 | + KeyEvent.KEYCODE_N to 'Т', |
| 102 | + KeyEvent.KEYCODE_M to 'Ь', |
| 103 | + // Removed direct mappings for Б and Ю |
| 104 | + KeyEvent.KEYCODE_SLASH to '.', |
| 105 | + KeyEvent.KEYCODE_BACKSLASH to '/', |
| 106 | + |
| 107 | + // Removed direct mapping for Ё |
| 108 | + ) |
| 109 | + |
| 110 | + /** |
| 111 | + * Alt-combo mappings used when Cyrillic layer is active and ALT is held. |
| 112 | + * 1) Alt+Y -> х |
| 113 | + * 2) Alt+U -> ъ |
| 114 | + * 3) Alt+G -> ж |
| 115 | + * 4) Alt+H -> э |
| 116 | + * 5) Alt+J -> ю |
| 117 | + * 6) Alt+K -> ё |
| 118 | + * 7) Alt+M -> б |
| 119 | + */ |
| 120 | + private val altLowercaseMap: Map<Int, Char> = mapOf( |
| 121 | + KeyEvent.KEYCODE_Y to 'х', |
| 122 | + KeyEvent.KEYCODE_U to 'ъ', |
| 123 | + KeyEvent.KEYCODE_G to 'ж', |
| 124 | + KeyEvent.KEYCODE_H to 'э', |
| 125 | + KeyEvent.KEYCODE_J to 'ю', |
| 126 | + KeyEvent.KEYCODE_K to 'ё', |
| 127 | + KeyEvent.KEYCODE_M to 'б', |
| 128 | + ) |
| 129 | + private val altUppercaseMap: Map<Int, Char> = mapOf( |
| 130 | + KeyEvent.KEYCODE_Y to 'Х', |
| 131 | + KeyEvent.KEYCODE_U to 'Ъ', |
| 132 | + KeyEvent.KEYCODE_G to 'Ж', |
| 133 | + KeyEvent.KEYCODE_H to 'Э', |
| 134 | + KeyEvent.KEYCODE_J to 'Ю', |
| 135 | + KeyEvent.KEYCODE_K to 'Ё', |
| 136 | + KeyEvent.KEYCODE_M to 'Б', |
| 137 | + ) |
| 138 | + |
| 139 | + /** |
| 140 | + * Get the Cyrillic character for the given key code and shift state |
| 141 | + * @param keyCode The key code to look up |
| 142 | + * @param isShifted Whether shift is active (uppercase) |
| 143 | + * @return The Cyrillic character, or null if no mapping exists |
| 144 | + */ |
| 145 | + fun getCyrillicChar(keyCode: Int, isShifted: Boolean): Char? { |
| 146 | + return if (isShifted) { |
| 147 | + uppercaseMap[keyCode] |
| 148 | + } else { |
| 149 | + lowercaseMap[keyCode] |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get the Cyrillic character for Alt+key when Cyrillic layer is active. |
| 155 | + */ |
| 156 | + fun getAltCyrillicChar(keyCode: Int, isShifted: Boolean): Char? { |
| 157 | + return if (isShifted) { |
| 158 | + altUppercaseMap[keyCode] |
| 159 | + } else { |
| 160 | + altLowercaseMap[keyCode] |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Check if a key code has a Cyrillic mapping |
| 166 | + * @param keyCode The key code to check |
| 167 | + * @return True if the key code has a Cyrillic mapping |
| 168 | + */ |
| 169 | + fun hasCyrillicMapping(keyCode: Int): Boolean { |
| 170 | + return lowercaseMap.containsKey(keyCode) |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Check if a key code has an ALT Cyrillic mapping |
| 175 | + */ |
| 176 | + fun hasAltCyrillicMapping(keyCode: Int): Boolean { |
| 177 | + return altLowercaseMap.containsKey(keyCode) |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get all supported key codes for Cyrillic input |
| 182 | + * @return Set of key codes that have Cyrillic mappings |
| 183 | + */ |
| 184 | + fun getSupportedKeyCodes(): Set<Int> = lowercaseMap.keys |
| 185 | +} |
0 commit comments