From 60a1c63fa9ae3540276aeb26d85b84ac37f713c7 Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Mon, 29 Oct 2018 17:30:58 +0100 Subject: [PATCH] Accepting non-alt-mod keys as characters in InputField if they don't have a function. Fixes #176 (hopefully) --- inputfield.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/inputfield.go b/inputfield.go index 86c9c20..3205d72 100644 --- a/inputfield.go +++ b/inputfield.go @@ -359,20 +359,26 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p i.cursorPos = len(i.text) - len(regexp.MustCompile(`^\s*\S+\s*`).ReplaceAllString(i.text[i.cursorPos:], "")) } + // Add character function. Returns whether or not the rune character is + // accepted. + add := func(r rune) bool { + newText := i.text[:i.cursorPos] + string(r) + i.text[i.cursorPos:] + if i.accept != nil { + return i.accept(newText, r) + } + i.text = newText + i.cursorPos += len(string(r)) + return true + } + // Process key event. switch key := event.Key(); key { case tcell.KeyRune: // Regular character. modifiers := event.Modifiers() if modifiers == tcell.ModNone { - ch := string(event.Rune()) - newText := i.text[:i.cursorPos] + ch + i.text[i.cursorPos:] - if i.accept != nil { - if !i.accept(newText, event.Rune()) { - break - } + if !add(event.Rune()) { + break } - i.text = newText - i.cursorPos += len(ch) } else if modifiers&tcell.ModAlt > 0 { // We accept some Alt- key combinations. switch event.Rune() { @@ -384,6 +390,10 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p moveWordLeft() case 'f': // Move word right. moveWordRight() + default: // Ignore Alt modifier for other keys. + if !add(event.Rune()) { + break + } } } case tcell.KeyCtrlU: // Delete all.