From 134a930f2ddb517f064afa528cc4f225f81e2b0c Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Mon, 29 Aug 2022 11:52:25 +0200 Subject: [PATCH] keys: properly support the alt modifier --- key.go | 11 ++++++----- key_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/key.go b/key.go index 07bc651..8ebaeeb 100644 --- a/key.go +++ b/key.go @@ -543,28 +543,29 @@ func readInputs(input io.Reader) ([]Msg, error) { // Is the alt key pressed? If so, the buffer will be prefixed with an // escape. + alt := false if len(runes) > 1 && runes[0] == 0x1b { - msgs = append(msgs, KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: runes[1:]})) - continue + alt = true + runes = runes[1:] } for _, v := range runes { // Is the first rune a control character? r := KeyType(v) if r <= keyUS || r == keyDEL { - msgs = append(msgs, KeyMsg(Key{Type: r})) + msgs = append(msgs, KeyMsg(Key{Type: r, Alt: alt})) continue } // If it's a space, override the type with KeySpace (but still include // the rune). if r == ' ' { - msgs = append(msgs, KeyMsg(Key{Type: KeySpace, Runes: []rune{v}})) + msgs = append(msgs, KeyMsg(Key{Type: KeySpace, Runes: []rune{v}, Alt: alt})) continue } // Welp, just regular, ol' runes. - msgs = append(msgs, KeyMsg(Key{Type: KeyRunes, Runes: []rune{v}})) + msgs = append(msgs, KeyMsg(Key{Type: KeyRunes, Runes: []rune{v}, Alt: alt})) } } diff --git a/key_test.go b/key_test.go index b74f06b..869b32f 100644 --- a/key_test.go +++ b/key_test.go @@ -134,6 +134,24 @@ func TestReadInput(t *testing.T) { }, }, }, + "alt+enter": { + []byte{'\x1b', '\r'}, + []Msg{ + KeyMsg{ + Type: KeyEnter, + Alt: true, + }, + }, + }, + "alt+ctrl+a": { + []byte{'\x1b', byte(keySOH)}, + []Msg{ + KeyMsg{ + Type: KeyCtrlA, + Alt: true, + }, + }, + }, } { t.Run(out, func(t *testing.T) { msgs, err := readInputs(bytes.NewReader(td.in))