Skip to content

Commit a85696d

Browse files
committed
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 zyedidia#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.
1 parent 9face74 commit a85696d

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

internal/action/bindings.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,31 +176,18 @@ modSearch:
176176
// see if the key is in bindingKeys with the Ctrl prefix.
177177
k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
178178
if code, ok := keyEvents["Ctrl"+k]; ok {
179-
var r tcell.Key
180-
// Special case for escape, for some reason tcell doesn't send it with the esc character
181-
if code < 256 && code != 27 {
182-
r = code
183-
}
184-
// It is, we're done.
185179
return KeyEvent{
186180
code: code,
187181
mod: modifiers,
188-
r: rune(r),
189182
}, true
190183
}
191184
}
192185

193186
// See if we can find the key in bindingKeys
194187
if code, ok := keyEvents[k]; ok {
195-
var r tcell.Key
196-
// Special case for escape, for some reason tcell doesn't send it with the esc character
197-
if code < 256 && code != 27 {
198-
r = code
199-
}
200188
return KeyEvent{
201189
code: code,
202190
mod: modifiers,
203-
r: rune(r),
204191
}, true
205192
}
206193

internal/action/bufpane.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ func (h *BufPane) HandleEvent(event tcell.Event) {
472472
ke := KeyEvent{
473473
code: e.Key(),
474474
mod: metaToAlt(e.Modifiers()),
475-
r: e.Rune(),
475+
}
476+
if e.Key() == tcell.KeyRune {
477+
ke.r = e.Rune()
476478
}
477479

478480
done := h.DoKeyEvent(ke)

internal/action/events.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (k KeyEvent) Name() string {
6868
if k.code == tcell.KeyRune {
6969
s = string(k.r)
7070
} else {
71-
s = fmt.Sprintf("Key[%d,%d]", k.code, int(k.r))
71+
s = fmt.Sprintf("Key[%d]", k.code)
7272
}
7373
}
7474
if len(m) != 0 {
@@ -155,11 +155,14 @@ func (m MouseEvent) Name() string {
155155
func ConstructEvent(event tcell.Event) (Event, error) {
156156
switch e := event.(type) {
157157
case *tcell.EventKey:
158-
return KeyEvent{
158+
ke := KeyEvent{
159159
code: e.Key(),
160160
mod: metaToAlt(e.Modifiers()),
161-
r: e.Rune(),
162-
}, nil
161+
}
162+
if e.Key() == tcell.KeyRune {
163+
ke.r = e.Rune()
164+
}
165+
return ke, nil
163166
case *tcell.EventRaw:
164167
return RawEvent{
165168
esc: e.EscSeq(),

internal/action/infopane.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func (h *InfoPane) HandleEvent(event tcell.Event) {
8989
ke := KeyEvent{
9090
code: e.Key(),
9191
mod: metaToAlt(e.Modifiers()),
92-
r: e.Rune(),
92+
}
93+
if e.Key() == tcell.KeyRune {
94+
ke.r = e.Rune()
9395
}
9496

9597
done := h.DoKeyEvent(ke)

internal/action/termpane.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ func (t *TermPane) HandleEvent(event tcell.Event) {
128128
ke := KeyEvent{
129129
code: e.Key(),
130130
mod: metaToAlt(e.Modifiers()),
131-
r: e.Rune(),
131+
}
132+
if e.Key() == tcell.KeyRune {
133+
ke.r = e.Rune()
132134
}
133135
action, more := TermBindings.NextEvent(ke, nil)
134136

0 commit comments

Comments
 (0)