From 116a0cfb8f37bd2547defc3643b69cf8eb7d6b1e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 31 Oct 2020 00:05:56 -0400 Subject: [PATCH] Process alt-modfied keystrokes before normal character input --- key.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/key.go b/key.go index 46b64e8..62422d9 100644 --- a/key.go +++ b/key.go @@ -306,6 +306,18 @@ func readInput(input io.Reader) (Msg, error) { return KeyMsg(k), nil } + // Is the alt key pressed? The buffer will be prefixed with an escape + // sequence if so. + if numBytes > 1 && buf[0] == 0x1b { + // Now remove the initial escape sequence and re-process to get the + // character being pressed in combination with alt. + c, _ := utf8.DecodeRune(buf[1:]) + if c == utf8.RuneError { + return nil, errors.New("could not decode rune after removing initial escape") + } + return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: []rune{c}}), nil + } + var runes []rune b := buf[:numBytes] @@ -329,24 +341,12 @@ func readInput(input io.Reader) (Msg, error) { return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil } - // Is it a control character? - char := runes[0] - if numBytes == 1 && char <= keyUS || char == keyDEL { - return KeyMsg(Key{Type: KeyType(char)}), nil + // Is the first rune a control character? + r := runes[0] + if numBytes == 1 && r <= keyUS || r == keyDEL { + return KeyMsg(Key{Type: KeyType(r)}), nil } - // Is the alt key pressed? The buffer will be prefixed with an escape - // sequence if so. - if numBytes > 1 && buf[0] == 0x1b { - // Now remove the initial escape sequence and re-process to get the - // character. - c, _ := utf8.DecodeRune(buf[1:]) - if c == utf8.RuneError { - return nil, errors.New("could not decode rune after removing initial escape") - } - return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: runes}), nil - } - - // Just a regular, ol' rune + // Welp, it's just a regular, ol' single rune return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil }