package main
import (
"strconv"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
func switchNextField(app *tview.Application, inputField [20]tview.Primitive) {
focusLabel := app.GetFocus().(*tview.InputField).GetLabel()
focusIndex, _ := strconv.Atoi(focusLabel)
if focusIndex == 19 {
focusIndex = 0
} else {
focusIndex = focusIndex + 1
}
app.SetFocus(inputField[focusIndex])
}
func switchLastField(app *tview.Application, inputField [20]tview.Primitive) {
focusLabel := app.GetFocus().(*tview.InputField).GetLabel()
focusIndex, _ := strconv.Atoi(focusLabel)
if focusIndex == 0 {
focusIndex = 19
} else {
focusIndex = focusIndex - 1
}
app.SetFocus(inputField[focusIndex])
}
func main() {
app := tview.NewApplication()
var inputField [20]tview.Primitive
var inputText [20]tview.Primitive
inputFlex := tview.NewFlex().SetDirection(tview.FlexRow)
for i := 0; i < 20; i++ {
inputField[i] = tview.NewInputField().SetLabel(strconv.Itoa(i)).
SetLabelWidth(15).SetText(strconv.Itoa(i)).SetFieldWidth(30)
inputText[i] = tview.NewTextView().SetText(strconv.Itoa(i))
inputFlex.AddItem(inputField[i], 0, 1, true).
AddItem(inputText[i], 0, 1, true)
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if _, ok := app.GetFocus().(*tview.InputField); ok {
switch event.Key() {
case tcell.KeyTab, tcell.KeyEnter, tcell.KeyDown:
switchNextField(app, inputField)
case tcell.KeyUp:
switchLastField(app, inputField)
}
}
return event
})
flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox(), 0, 1, false).AddItem(inputFlex, 0, 1, true).
AddItem(tview.NewBox(), 0, 1, false)
if err := app.SetRoot(flex, true).Run(); err != nil {
panic(err)
}
}
Run the code, only the last few input field are displayed on the screen.

How to display the focused field when press Up or Down key?
If you have answer and discuss this issue, please tell me. Thanks
I need multiple input field and switch them with pressing Up or Down key
This code is shown below
Run the code, only the last few input field are displayed on the screen.

How to display the focused field when press Up or Down key?
If you have answer and discuss this issue, please tell me. Thanks