1
1
mirror of https://github.com/walles/moar.git synced 2024-09-11 20:17:13 +03:00
moar/twin/keys.go
Ilya Grigoriev 7b8d7eafe3 keys.go: Allow alternative Home/End key encoding
My setup is Tmux running in Alacritty on Debian Linux running on Chrome OS.
Before this change, Home and End keys did not work for me inside Tmux.
(Outside tmux, I don't think there was a problem).

I got the required sequences by running `cat` and pressing Home/End.

This looks similar to (or possibly the opposite of)
daafbcdac4.
The reference in that commit mentions these sequences for Home and End, these
kinds of sequences were already used for Delete, PgUp, and PgDown. 

For reference, the missing `\x1b[2~`  sequence would correspond to the Insert
key.
2023-09-29 23:30:38 -07:00

72 lines
1.5 KiB
Go

package twin
type KeyCode uint16
const (
KeyEscape KeyCode = iota
KeyEnter
KeyBackspace
KeyDelete
KeyUp
KeyDown
KeyRight
KeyLeft
KeyAltUp
KeyAltDown
KeyAltRight
KeyAltLeft
KeyHome
KeyEnd
KeyPgUp
KeyPgDown
)
// Map incoming escape keystrokes to keycodes, used in consumeEncodedEvent() in
// screen.go.
//
// NOTE: If you put a single ESC character in here ('\x1b') it will be consumed
// by itself rather than as part of the sequence it belongs to, and parsing of
// all special sequences starting with ESC will break down.
//
// FIXME: Write a test preventing that from happening.
var escapeSequenceToKeyCode = map[string]KeyCode{
// NOTE: Please keep this list in the same order as the KeyCode const()
// section above.
// KeyEscape intentionally left out because it's too short, see comment
// above.
// KeyEnter intentionally left out because it's too short, see comment
// above.
"\x7f": KeyBackspace,
"\x1b[3~": KeyDelete,
"\x1b[A": KeyUp,
"\x1b[B": KeyDown,
"\x1b[C": KeyRight,
"\x1b[D": KeyLeft,
// Ref: https://github.com/walles/moar/issues/138#issuecomment-1579199274
"\x1bOA": KeyUp,
"\x1bOB": KeyDown,
"\x1bOC": KeyRight,
"\x1bOD": KeyLeft,
"\x1b\x1b[A": KeyAltUp, // Alt + up arrow
"\x1b\x1b[B": KeyAltDown, // Alt + down arrow
"\x1b\x1b[C": KeyAltRight, // Alt + right arrow
"\x1b\x1b[D": KeyAltLeft, // Alt + left arrow
"\x1b[H": KeyHome,
"\x1b[F": KeyEnd,
"\x1b[1~": KeyHome,
"\x1b[4~": KeyEnd,
"\x1b[5~": KeyPgUp,
"\x1b[6~": KeyPgDown,
}