Don't use tcell's Rune() for non-KeyRune events

According to tcell documentation, Rune() should only be used for KeyRune
events. Otherwise its return value is not guaranteed and should not be
relied upon.

This fixes issue #2947: Esc key not working on Windows, since tcell
sends lone Esc key event with rune == 0 on Unix but with rune == 27
(the keycode) on Windows.
This commit is contained in:
Dmytro Maluka
2024-06-10 02:30:55 +02:00
parent 9face7484e
commit a85696d5e0
5 changed files with 16 additions and 20 deletions

View File

@@ -176,31 +176,18 @@ modSearch:
// see if the key is in bindingKeys with the Ctrl prefix.
k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
if code, ok := keyEvents["Ctrl"+k]; ok {
var r tcell.Key
// Special case for escape, for some reason tcell doesn't send it with the esc character
if code < 256 && code != 27 {
r = code
}
// It is, we're done.
return KeyEvent{
code: code,
mod: modifiers,
r: rune(r),
}, true
}
}
// See if we can find the key in bindingKeys
if code, ok := keyEvents[k]; ok {
var r tcell.Key
// Special case for escape, for some reason tcell doesn't send it with the esc character
if code < 256 && code != 27 {
r = code
}
return KeyEvent{
code: code,
mod: modifiers,
r: rune(r),
}, true
}

View File

@@ -472,7 +472,9 @@ func (h *BufPane) HandleEvent(event tcell.Event) {
ke := KeyEvent{
code: e.Key(),
mod: metaToAlt(e.Modifiers()),
r: e.Rune(),
}
if e.Key() == tcell.KeyRune {
ke.r = e.Rune()
}
done := h.DoKeyEvent(ke)

View File

@@ -68,7 +68,7 @@ func (k KeyEvent) Name() string {
if k.code == tcell.KeyRune {
s = string(k.r)
} else {
s = fmt.Sprintf("Key[%d,%d]", k.code, int(k.r))
s = fmt.Sprintf("Key[%d]", k.code)
}
}
if len(m) != 0 {
@@ -155,11 +155,14 @@ func (m MouseEvent) Name() string {
func ConstructEvent(event tcell.Event) (Event, error) {
switch e := event.(type) {
case *tcell.EventKey:
return KeyEvent{
ke := KeyEvent{
code: e.Key(),
mod: metaToAlt(e.Modifiers()),
r: e.Rune(),
}, nil
}
if e.Key() == tcell.KeyRune {
ke.r = e.Rune()
}
return ke, nil
case *tcell.EventRaw:
return RawEvent{
esc: e.EscSeq(),

View File

@@ -89,7 +89,9 @@ func (h *InfoPane) HandleEvent(event tcell.Event) {
ke := KeyEvent{
code: e.Key(),
mod: metaToAlt(e.Modifiers()),
r: e.Rune(),
}
if e.Key() == tcell.KeyRune {
ke.r = e.Rune()
}
done := h.DoKeyEvent(ke)

View File

@@ -128,7 +128,9 @@ func (t *TermPane) HandleEvent(event tcell.Event) {
ke := KeyEvent{
code: e.Key(),
mod: metaToAlt(e.Modifiers()),
r: e.Rune(),
}
if e.Key() == tcell.KeyRune {
ke.r = e.Rune()
}
action, more := TermBindings.NextEvent(ke, nil)