Skip to content
Myr

keys: properly support the alt modifier

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Raphael 'kena' Poss · committed by Christian Muehlhaeuser · parent f905b97 · Browse files · 2 files+24 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−5 removed

  • other:+24 added−5 removed

2 files+24 added−5 removed

modified:key_test.go+18 added
132 unmodified lines
133 Type: KeyShiftTab,134 },135 },136 },137 "alt+enter": {138 []byte{'\x1b', '\r'},139 []Msg{140 KeyMsg{141 Type: KeyEnter,142 Alt: true,143 },144 },145 },146 "alt+ctrl+a": {147 []byte{'\x1b', byte(keySOH)},148 []Msg{149 KeyMsg{150 Type: KeyCtrlA,151 Alt: true,152 },153 },154 },155 } {156 t.Run(out, func(t *testing.T) {157 msgs, err := readInputs(bytes.NewReader(td.in))158 if err != nil {
27 unmodified lines
modified:key.go+6 added-5 removed
541 unmodified lines
542 }543544 // Is the alt key pressed? If so, the buffer will be prefixed with an545 // escape.546 alt := false547 if len(runes) > 1 && runes[0] == 0x1b {547 msgs = append(msgs, KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: runes[1:]}))548 continue548 alt = true549 runes = runes[1:]550 }551552 for _, v := range runes {553 // Is the first rune a control character?554 r := KeyType(v)555 if r <= keyUS || r == keyDEL {555 msgs = append(msgs, KeyMsg(Key{Type: r}))556 msgs = append(msgs, KeyMsg(Key{Type: r, Alt: alt}))557 continue558 }559560 // If it's a space, override the type with KeySpace (but still include561 // the rune).562 if r == ' ' {562 msgs = append(msgs, KeyMsg(Key{Type: KeySpace, Runes: []rune{v}}))563 msgs = append(msgs, KeyMsg(Key{Type: KeySpace, Runes: []rune{v}, Alt: alt}))564 continue565 }566567 // Welp, just regular, ol' runes.567 msgs = append(msgs, KeyMsg(Key{Type: KeyRunes, Runes: []rune{v}}))568 msgs = append(msgs, KeyMsg(Key{Type: KeyRunes, Runes: []rune{v}, Alt: alt}))569 }570 }571572 return msgs, nil
1 unmodified line