From ca43f95fb1835f74f837e1599a3383b4a7123390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Ov=C4=8D=C3=A1=C4=8D=C3=ADk?= Date: Tue, 21 Sep 2021 11:09:04 +0200 Subject: [PATCH] Fix Backspace key to emit BSpace and AltBS events instead of CtrlH CtrlH events are still sent when appropriate. I have adjusted FullscreenRenderer to match the LightRenderer's behaviour, which seems to be correct. --- src/tui/tcell.go | 17 ++++++++++++++++- src/tui/tcell_test.go | 19 ++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/tui/tcell.go b/src/tui/tcell.go index b5f788a..dd0581b 100644 --- a/src/tui/tcell.go +++ b/src/tui/tcell.go @@ -223,6 +223,7 @@ func (r *FullscreenRenderer) GetChar() Event { // process keyboard: case *tcell.EventKey: mods := ev.Modifiers() + none := mods == tcell.ModNone alt := (mods & tcell.ModAlt) > 0 ctrl := (mods & tcell.ModCtrl) > 0 shift := (mods & tcell.ModShift) > 0 @@ -252,7 +253,21 @@ func (r *FullscreenRenderer) GetChar() Event { case tcell.KeyCtrlG: return keyfn('g') case tcell.KeyCtrlH: - return keyfn('h') + switch ev.Rune() { + case 0: + if ctrl { + return Event{BSpace, 0, nil} + } + case rune(tcell.KeyCtrlH): + switch { + case ctrl: + return keyfn('h') + case alt: + return Event{AltBS, 0, nil} + case none, shift: + return Event{BSpace, 0, nil} + } + } case tcell.KeyCtrlI: return keyfn('i') case tcell.KeyCtrlJ: diff --git a/src/tui/tcell_test.go b/src/tui/tcell_test.go index 1569ae6..aa63b72 100644 --- a/src/tui/tcell_test.go +++ b/src/tui/tcell_test.go @@ -107,16 +107,17 @@ func TestGetCharEventKey(t *testing.T) { {giveKey{tcell.KeyDEL, 0, tcell.ModNone}, wantKey{BSpace, 0, nil}}, // fabricated, unhandled {giveKey{tcell.KeyDelete, 0, tcell.ModNone}, wantKey{Del, 0, nil}}, {giveKey{tcell.KeyDelete, 0, tcell.ModAlt}, wantKey{Del, 0, nil}}, - {giveKey{tcell.KeyBackspace, 0, tcell.ModNone}, wantKey{CtrlH, 0, nil}}, // fabricated, unhandled - {giveKey{tcell.KeyBS, 0, tcell.ModNone}, wantKey{CtrlH, 0, nil}}, // fabricated, unhandled - {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModNone}, wantKey{CtrlH, 0, nil}}, // actual "Backspace" keystroke - {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModAlt}, wantKey{CtrlAlt, 'h', nil}}, // actual "Alt+Backspace" keystroke + {giveKey{tcell.KeyBackspace, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled + {giveKey{tcell.KeyBS, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled + {giveKey{tcell.KeyCtrlH, 0, tcell.ModNone}, wantKey{Invalid, 0, nil}}, // fabricated, unhandled + {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModNone}, wantKey{BSpace, 0, nil}}, // actual "Backspace" keystroke + {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModAlt}, wantKey{AltBS, 0, nil}}, // actual "Alt+Backspace" keystroke {giveKey{tcell.KeyDEL, rune(tcell.KeyDEL), tcell.ModCtrl}, wantKey{BSpace, 0, nil}}, // actual "Ctrl+Backspace" keystroke - {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift}, wantKey{CtrlH, 0, nil}}, // actual "Shift+Backspace" keystroke - {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAlt, 'h', nil}}, // actual "Ctrl+Alt+Backspace" keystroke - {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlH, 0, nil}}, // actual "Ctrl+Shift+Backspace" keystroke - {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift | tcell.ModAlt}, wantKey{CtrlAlt, 'h', nil}}, // actual "Shift+Alt+Backspace" keystroke - {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt | tcell.ModShift}, wantKey{CtrlAlt, 'h', nil}}, // actual "Ctrl+Shift+Alt+Backspace" keystroke + {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift}, wantKey{BSpace, 0, nil}}, // actual "Shift+Backspace" keystroke + {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt}, wantKey{BSpace, 0, nil}}, // actual "Ctrl+Alt+Backspace" keystroke + {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModShift}, wantKey{BSpace, 0, nil}}, // actual "Ctrl+Shift+Backspace" keystroke + {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModShift | tcell.ModAlt}, wantKey{AltBS, 0, nil}}, // actual "Shift+Alt+Backspace" keystroke + {giveKey{tcell.KeyCtrlH, 0, tcell.ModCtrl | tcell.ModAlt | tcell.ModShift}, wantKey{BSpace, 0, nil}}, // actual "Ctrl+Shift+Alt+Backspace" keystroke {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl}, wantKey{CtrlH, 0, nil}}, // actual "Ctrl+H" keystroke {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl | tcell.ModAlt}, wantKey{CtrlAlt, 'h', nil}}, // fabricated "Ctrl+Alt+H" keystroke {giveKey{tcell.KeyCtrlH, rune(tcell.KeyCtrlH), tcell.ModCtrl | tcell.ModShift}, wantKey{CtrlH, 0, nil}}, // actual "Ctrl+Shift+H" keystroke