fix: support kitty keyboard protocol for ctrl keybindings (#101)
Fixes #88 ## Root cause `MatchKey` treated `Ctrl+M` and the Enter key as the same raw byte (`0x0d`). After editing the generated config, a `ctrl+m` binding could shadow Enter, causing Return to toggle mode instead of executing the selected command. While investigating this, I also found that `MatchKey` only recognized raw control bytes for `ctrl+<letter>` bindings. In Fish and terminals that use the Kitty keyboard protocol (e.g. Kitty and Foot), those key combinations may instead be sent as CSI sequences such as `\x1b[106;5u`, preventing navigation bindings like `ctrl+j` / `ctrl+k` from being matched. ## Changes * `internal/config/keys.go` * Reserve the Enter byte (`0x0d`) so it cannot be claimed by a `ctrl+m` keybinding. * Add `matchKittyCtrl()` to support Kitty keyboard protocol CSI sequences for `ctrl+<letter>` bindings when the Ctrl modifier is present. * `root/wrapper.go` * Extract `handleNavKey()` to remove duplicated navigation handling logic. * `internal/config/config_test.go` * Add regression tests covering the Enter/`ctrl+m` fix and Kitty keyboard protocol key sequences. ## Tested * `go test ./...` — 201 passed * Verified that Enter is no longer shadowed by `ctrl+m` after editing the generated config. * Verified that `ctrl+j` / `ctrl+k` navigation works correctly in Fish. --------- Co-authored-by: verse91 <versedev.store@proton.me> Co-authored-by: VERSE <sunnyone0901@gmail.com>
This commit is contained in:
co-authored by
verse91
VERSE
parent
6faa2e3274
commit
d669e97423
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -29,9 +30,25 @@ func MatchKey(input []byte, expected string) (matched bool, consumed int) {
|
||||
char := expected[5]
|
||||
if char >= 'a' && char <= 'z' {
|
||||
targetByte := char - 'a' + 1
|
||||
// 0x0d ('m') is the Carriage Return byte. In a raw terminal the
|
||||
// Enter/Return key and Ctrl+M both arrive as 0x0d, so they are
|
||||
// indistinguishable. Matching a "ctrl+m" keybinding here would let
|
||||
// it shadow the Enter key and break line submission (the wrapper
|
||||
// checks keybindings before its enter handler). Reserve this byte
|
||||
// for Enter so a "ctrl+m" keybinding can never hijack it.
|
||||
if targetByte == 0x0d {
|
||||
return false, 0
|
||||
}
|
||||
if input[0] == targetByte {
|
||||
return true, 1
|
||||
}
|
||||
// Some terminals (foot, kitty, etc.) send kitty keyboard protocol
|
||||
// escape sequences for Ctrl+letter instead of raw control bytes:
|
||||
// CSI <keycode> ; <modifiers> <action>
|
||||
// where modifiers include Ctrl=4 and action='u' means press.
|
||||
if matched, consumed := matchKittyCtrl(input, int(char)); matched {
|
||||
return matched, consumed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +92,51 @@ func MatchKey(input []byte, expected string) (matched bool, consumed int) {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
// matchKittyCtrl matches the kitty keyboard protocol CSI sequence for Ctrl+<letter>.
|
||||
// Format: ESC [ <keycode> ; <modifiers> <action>
|
||||
// For Ctrl+<letter>: keycode is the ASCII code of the letter, modifiers must have bit 2 (value 4) set, action is 'u'.
|
||||
func matchKittyCtrl(input []byte, expectedASCII int) (matched bool, consumed int) {
|
||||
if len(input) < 6 || input[0] != 0x1b || input[1] != '[' {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
uIdx := -1
|
||||
for i := 2; i < len(input); i++ {
|
||||
if input[i] == 'u' {
|
||||
uIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if uIdx == -1 {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
body := string(input[2:uIdx])
|
||||
parts := strings.Split(body, ";")
|
||||
if len(parts) != 2 {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
keycode, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return false, 0
|
||||
}
|
||||
modifiers, err := strconv.Atoi(parts[1])
|
||||
if err != nil {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
if modifiers&4 == 0 {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
if keycode != expectedASCII {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
return true, uIdx + 1
|
||||
}
|
||||
|
||||
// FormatKeyName takes a config key string like "ctrl+r" and formats it for UI display, e.g. "<Ctrl+R>".
|
||||
func FormatKeyName(key string) string {
|
||||
parts := strings.Split(key, "+")
|
||||
|
||||
Reference in New Issue
Block a user