Skip to content

Commit

Permalink
Process alt-modfied keystrokes before normal character input
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Nov 1, 2020
1 parent 1b5f599 commit 116a0cf
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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 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
// Is the first rune a control character?
r := runes[0]
if numBytes == 1 && r <= keyUS || r == keyDEL {
return KeyMsg(Key{Type: KeyType(r)}), nil
}

// Just a regular, ol' rune
// Welp, it's just a regular, ol' single rune
return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil
}

0 comments on commit 116a0cf

Please sign in to comment.