diff --git a/pkg/gocui/edit.go b/pkg/gocui/edit.go index 649379fb6..4263e0b5c 100644 --- a/pkg/gocui/edit.go +++ b/pkg/gocui/edit.go @@ -78,7 +78,7 @@ func SimpleEditor(v *View, key Key) bool { v.TextArea.GoToEndOfLine() case key.Equals(NewKeyStrMod("y", ModCtrl)): v.TextArea.Yank() - case key.Str() != "" && key.Mod() == 0: + case key.IsPrintable(): v.TextArea.TypeCharacter(key.Str()) default: return false diff --git a/pkg/gocui/gui.go b/pkg/gocui/gui.go index 9d290c8ad..ba4c7e74e 100644 --- a/pkg/gocui/gui.go +++ b/pkg/gocui/gui.go @@ -2097,7 +2097,7 @@ func (g *Gui) matchView(v *View, kb *keybinding) bool { if v == nil { return false } - if v.Editable && kb.key.Str() != "" && kb.key.Mod() == 0 { + if v.Editable && kb.key.IsPrintable() { return false } if kb.viewName != v.name { diff --git a/pkg/gocui/key.go b/pkg/gocui/key.go index dd0a912a0..0eaa29b38 100644 --- a/pkg/gocui/key.go +++ b/pkg/gocui/key.go @@ -61,6 +61,12 @@ func (k Key) IsSet() bool { return k.keyName != 0 } +// IsPrintable reports whether the key stands for a character that can be typed +// into a text field. +func (k Key) IsPrintable() bool { + return k.keyName == KeyName(tcell.KeyRune) && k.str != "" && k.mod == ModNone +} + func (k Key) Equals(otherKey Key) bool { return k.keyName == otherKey.keyName && k.str == otherKey.str && k.mod == otherKey.mod } diff --git a/pkg/gocui/key_test.go b/pkg/gocui/key_test.go new file mode 100644 index 000000000..8fce3fe53 --- /dev/null +++ b/pkg/gocui/key_test.go @@ -0,0 +1,16 @@ +package gocui + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKeyIsPrintable(t *testing.T) { + assert.True(t, NewKeyRune('x').IsPrintable()) + assert.True(t, NewKeyRune('界').IsPrintable()) + assert.True(t, NewKeyRune(' ').IsPrintable()) + assert.False(t, NewKeyStrMod("x", ModCtrl).IsPrintable()) + assert.False(t, NewKeyName(KeyEnter).IsPrintable()) + assert.False(t, Key{}.IsPrintable()) +}