From 0cb138f63d6b452fec8146981fa3f9e644bb366f Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 19 Feb 2020 21:06:57 -0500 Subject: [PATCH] Catch a bunch of outlier alt-prefixed keystokes + home/end/pgup/pgdown --- key.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/key.go b/key.go index 72c2106..d208cb8 100644 --- a/key.go +++ b/key.go @@ -136,6 +136,10 @@ const ( KeyRight KeyLeft KeyShiftTab + KeyHome + KeyEnd + KeyPgUp + KeyPgDown ) // Mapping for control keys to friendly consts @@ -181,6 +185,10 @@ var keyNames = map[int]string{ KeyRight: "right", KeyLeft: "left", KeyShiftTab: "shift+tab", + KeyHome: "home", + KeyEnd: "end", + KeyPgUp: "pgup", + KeyPgDown: "pgdown", } // Mapping for sequences to consts @@ -191,6 +199,26 @@ var sequences = map[string]KeyType{ "\x1b[D": KeyLeft, } +// Mapping for hex codes to consts. Unclear why these won't register as +// sequences. +var hexes = map[string]Key{ + "1b5b5a": Key{Type: KeyShiftTab}, + "1b0d": Key{Alt: true, Type: KeyEnter}, + "1b7f": Key{Alt: true, Type: KeyDelete}, + "1b5b48": Key{Type: KeyHome}, + "1b5b313b3348": Key{Alt: true, Type: KeyHome}, + "1b5b46": Key{Type: KeyEnd}, + "1b5b313b3346": Key{Alt: true, Type: KeyEnd}, + "1b5b357e": Key{Type: KeyPgUp}, + "1b5b353b337e": Key{Alt: true, Type: KeyPgUp}, + "1b5b367e": Key{Type: KeyPgDown}, + "1b5b363b337e": Key{Alt: true, Type: KeyPgDown}, + "1b5b313b3341": Key{Alt: true, Type: KeyUp}, + "1b5b313b3342": Key{Alt: true, Type: KeyDown}, + "1b5b313b3343": Key{Alt: true, Type: KeyRight}, + "1b5b313b3344": Key{Alt: true, Type: KeyLeft}, +} + // ReadKey reads keypress input from a TTY and returns a string representation // of a key func ReadKey(r io.Reader) (Key, error) { @@ -202,9 +230,11 @@ func ReadKey(r io.Reader) (Key, error) { return Key{}, err } - // Shift+tab needs some very special handling - if "1b5b5a" == fmt.Sprintf("%x", buf[:numBytes]) { - return Key{Type: KeyShiftTab}, nil + hex := fmt.Sprintf("%x", buf[:numBytes]) + + // Some of these need special handling + if k, ok := hexes[hex]; ok { + return k, nil } // Get unicode value