Skip to content
Myr

Process alt-modfied keystrokes before normal character input

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

authored by Christian Rocha · committed by Christian Rocha · parent 1b5f599 · Browse files · 1 file+17 added−17 removed
This commit

State

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

Diff stats

1 file+17 added−17 removed

  • other:+17 added−17 removed

1 file+17 added−17 removed

modified:key.go+17 added-17 removed
304 unmodified lines
305 if k, ok := hexes[hex]; ok {306 return KeyMsg(k), nil307 }308309 // Is the alt key pressed? The buffer will be prefixed with an escape310 // sequence if so.311 if numBytes > 1 && buf[0] == 0x1b {312 // Now remove the initial escape sequence and re-process to get the313 // character being pressed in combination with alt.314 c, _ := utf8.DecodeRune(buf[1:])315 if c == utf8.RuneError {316 return nil, errors.New("could not decode rune after removing initial escape")317 }318 return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: []rune{c}}), nil319 }320321 var runes []rune322 b := buf[:numBytes]323324 // Translate input into runes. In most cases we'll receive exactly one
15 unmodified lines
340 // character, sequence, and so on.341 return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil342 }343332 // Is it a control character?333 char := runes[0]334 if numBytes == 1 && char <= keyUS || char == keyDEL {335 return KeyMsg(Key{Type: KeyType(char)}), nil344 // Is the first rune a control character?345 r := runes[0]346 if numBytes == 1 && r <= keyUS || r == keyDEL {347 return KeyMsg(Key{Type: KeyType(r)}), nil348 }349338 // Is the alt key pressed? The buffer will be prefixed with an escape339 // sequence if so.340 if numBytes > 1 && buf[0] == 0x1b {341 // Now remove the initial escape sequence and re-process to get the342 // character.343 c, _ := utf8.DecodeRune(buf[1:])344 if c == utf8.RuneError {345 return nil, errors.New("could not decode rune after removing initial escape")346 }347 return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: runes}), nil348 }349350 // Just a regular, ol' rune350 // Welp, it's just a regular, ol' single rune351 return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil352}