Accepting non-alt-mod keys as characters in InputField if they don't have a function. Fixes #176 (hopefully)

This commit is contained in:
Oliver 2018-10-29 17:30:58 +01:00
parent 380278f41c
commit 60a1c63fa9
1 changed files with 18 additions and 8 deletions

View File

@ -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.