package keybind import ( "strconv" "strings" "unicode/utf8" ) // Key recording. // // While recording, the editor asks the terminal for the kitty keyboard // protocol (flags 1|8: disambiguate escape codes, report all keys as escape // codes). Ghostty then reports every key press, including ones with the // super/cmd modifier, as "CSI code ; modifiers u" (or the legacy // "CSI 1 ; modifiers A" forms for arrows and function keys). Terminals // without the protocol send classic sequences, which are decoded from the // key names Bubble Tea produces. // PushKitty and PopKitty enable and restore the kitty keyboard protocol. // QueryKitty asks the terminal for the current flags; a terminal that // supports the protocol answers with "CSI ? flags u". const ( PushKitty = "\x1b[>9u" PopKitty = "\x1b[ 1 { modField = params[1] } c := Combo{Mods: decodeMods(modField)} switch { case final == 'u': codeStr, _, _ := strings.Cut(params[0], ":") // drop shifted/base-layout keys code, err := strconv.Atoi(codeStr) if err != nil { return Combo{}, false } if code >= 57441 && code <= 57452 { c.Modifier = true // shift, ctrl, alt, super, hyper, meta, … keys return c, true } if name, ok := kittyFunctional[code]; ok { c.Key = name if code == 57358 || code == 57360 { c.Modifier = true // lock keys } return c, true } if code < 32 || !utf8.ValidRune(rune(code)) { return Combo{}, false } c.Key = string(rune(code)) return c, true case final == '~': code, err := strconv.Atoi(params[0]) if err != nil { return Combo{}, false } name, ok := tildeKeys[code] if !ok { return Combo{}, false } c.Key = name return c, true default: name, ok := legacyFinal[final] if !ok { return Combo{}, false } c.Key = name return c, true } } var legacyNames = map[string]string{ "up": "arrow_up", "down": "arrow_down", "left": "arrow_left", "right": "arrow_right", "pgup": "page_up", "pgdown": "page_down", "home": "home", "end": "end", "insert": "insert", "delete": "delete", "tab": "tab", "enter": "enter", "esc": "escape", "backspace": "backspace", " ": "space", "space": "space", } // DecodeKeyName converts a Bubble Tea key name ("ctrl+a", "alt+shift+up", // "f5", "A") into a combination. Classic terminal input cannot represent // super/cmd, and shifted symbols arrive as the symbol itself. func DecodeKeyName(name string) (Combo, bool) { c := Combo{Mods: map[string]bool{}} if name == "" { return c, false } switch name { case "shift+tab": c.Mods["shift"] = true c.Key = "tab" return c, true case "ctrl+@", "ctrl+ ": c.Mods["ctrl"] = true c.Key = "space" return c, true } rest := name for { found := false for _, m := range []string{"ctrl+", "alt+", "shift+"} { if strings.HasPrefix(rest, m) && len(rest) > len(m) { c.Mods[strings.TrimSuffix(m, "+")] = true rest = rest[len(m):] found = true } } if !found { break } } if k, ok := legacyNames[rest]; ok { c.Key = k return c, true } if len(rest) >= 2 && rest[0] == 'f' { if n, err := strconv.Atoi(rest[1:]); err == nil && n >= 1 && n <= 25 { c.Key = rest return c, true } } if utf8.RuneCountInString(rest) == 1 { r, _ := utf8.DecodeRuneInString(rest) if r >= 'A' && r <= 'Z' { c.Mods["shift"] = true r += 'a' - 'A' } c.Key = string(r) return c, true } return c, false }