NormalizeEvent converts an EventKey into a canonical (key,rune,mod) triple. Ctrl+A style events are normalized to KeyRune with the corresponding rune.
(ev *tcell.EventKey)
| 182 | // NormalizeEvent converts an EventKey into a canonical (key,rune,mod) triple. |
| 183 | // Ctrl+A style events are normalized to KeyRune with the corresponding rune. |
| 184 | func NormalizeEvent(ev *tcell.EventKey) (tcell.Key, rune, tcell.ModMask) { |
| 185 | key := ev.Key() |
| 186 | r := ev.Rune() |
| 187 | mod := ev.Modifiers() |
| 188 | |
| 189 | if key == tcell.KeyCtrlI { |
| 190 | key = tcell.KeyTab |
| 191 | mod |= tcell.ModCtrl |
| 192 | } |
| 193 | |
| 194 | // Normalize Shift+Tab across terminals and tcell versions. |
| 195 | // Some terminals produce KeyBacktab, others produce KeyTab with ModShift. |
| 196 | if key == tcell.KeyBacktab { |
| 197 | // KeyBacktab already implies Shift. |
| 198 | mod &^= tcell.ModShift |
| 199 | } else if key == tcell.KeyTab { |
| 200 | other := mod &^ tcell.ModShift |
| 201 | if (mod&tcell.ModShift) != 0 && other == 0 { |
| 202 | key = tcell.KeyBacktab |
| 203 | mod = 0 |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // If a rune is uppercase, the shift modifier should be active. |
| 208 | isRuneKey := key == tcell.KeyRune |
| 209 | if isRuneKey { |
| 210 | if unicode.IsUpper(r) { |
| 211 | mod |= tcell.ModShift |
| 212 | } |
| 213 | |
| 214 | if _, ok := shiftedDigits[r]; ok { |
| 215 | mod |= tcell.ModShift |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Normalize Ctrl+<char> keys to a rune and ModCtrl. This makes bindings |
| 220 | // more consistent, as some terminals send KeyCtrlX and others send a rune |
| 221 | // with a Ctrl modifier. We must preserve existing modifiers. |
| 222 | ctrlRune, isCtrlKey := ToChar(key) |
| 223 | if isCtrlKey { |
| 224 | key = tcell.KeyRune |
| 225 | r = ctrlRune |
| 226 | mod |= tcell.ModCtrl |
| 227 | } |
| 228 | |
| 229 | if key == tcell.KeyRune { |
| 230 | if u, ok := shiftedDigits[r]; ok { |
| 231 | r = u |
| 232 | } |
| 233 | |
| 234 | r = unicode.ToLower(r) |
| 235 | } else { |
| 236 | // Non-rune keys shouldn't leak any underlying rune value tcell sets |
| 237 | // (e.g., KeyTab may carry 'i' in newer tcell versions). |
| 238 | r = 0 |
| 239 | } |
| 240 | |
| 241 | return key, r, mod |