Properly catch shift+tab

This commit is contained in:
Christian Rocha 2020-02-18 14:52:57 -05:00
parent d38a22380b
commit 3c6a2c1a52
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 21 additions and 9 deletions

30
key.go
View File

@ -2,6 +2,7 @@ package tea
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"unicode/utf8" "unicode/utf8"
) )
@ -127,6 +128,7 @@ const (
KeyDown KeyDown
KeyRight KeyRight
KeyLeft KeyLeft
KeyShiftTab
) )
// Mapping for control keys to friendly consts // Mapping for control keys to friendly consts
@ -166,19 +168,21 @@ var keyNames = map[int]string{
keySP: "space", keySP: "space",
keyDEL: "delete", keyDEL: "delete",
KeyRune: "rune", KeyRune: "rune",
KeyUp: "up", KeyUp: "up",
KeyDown: "down", KeyDown: "down",
KeyRight: "right", KeyRight: "right",
KeyLeft: "left", KeyLeft: "left",
KeyShiftTab: "shift+tab",
} }
// Mapping for sequences to consts // Mapping for sequences to consts
var sequences = map[string]KeyType{ var sequences = map[string]KeyType{
"\x1b[A": KeyUp, "\x1b[A": KeyUp,
"\x1b[B": KeyDown, "\x1b[B": KeyDown,
"\x1b[C": KeyRight, "\x1b[C": KeyRight,
"\x1b[D": KeyLeft, "\x1b[D": KeyLeft,
"\x1b5B5A": KeyShiftTab,
} }
// ReadKey reads keypress input from a TTY and returns a string representation // ReadKey reads keypress input from a TTY and returns a string representation
@ -192,6 +196,14 @@ func ReadKey(r io.Reader) (Key, error) {
return Key{}, err return Key{}, err
} }
// Hex representation of input sequence
hex := fmt.Sprintf("%x", buf[:n])
switch hex {
case "1b5b5a":
return Key{Type: KeyShiftTab}, nil
}
// Get rune // Get rune
c, _ := utf8.DecodeRune(buf[:]) c, _ := utf8.DecodeRune(buf[:])
if c == utf8.RuneError { if c == utf8.RuneError {