[GH-ISSUE #922] Form.SetFocus(item index) does not seem to work #673

Closed
opened 2026-03-04 01:06:57 +03:00 by kerem · 1 comment
Owner

Originally created by @elsni on GitHub (Nov 25, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/922

In a form, I want to move between FormItems using PgUp and PgDown in addition to Tab/Shift-Tab.
So I implemented an InputCapture func to do this, but setFocus does not seem to do anything.
Maybe I made a stupid mistake.

used version: commit 7c9e464

code:

func TestForm() {
	app := tview.NewApplication()
	form := tview.NewForm().
		AddInputField("Monday", "Test", 40, nil, nil).
		AddInputField("Tuesday", "Test", 40, nil, nil).
		AddInputField("Wednesday", "Test", 40, nil, nil).
		AddInputField("Thursday", "Test", 40, nil, nil)
	form.AddButton("Quit", func() {
		app.Stop()
	})
	form.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyEscape:
			app.Stop()
			return nil
		case tcell.KeyPgDn:
			fallthrough
		case tcell.KeyPgUp:
			maxidx := form.GetFormItemCount() - 1
			fidx, _ := form.GetFocusedItemIndex()
			// focused itemindex is -1 when a button is focused
			if fidx > -1 {
				if event.Key() == tcell.KeyPgDn {
					fidx += 1
					if fidx > maxidx {
						fidx = 0
					}
				}
				if event.Key() == tcell.KeyPgUp {
					fidx -= 1
					if fidx < 0 {
						fidx = maxidx
					}
				}

				form.SetFocus(fidx)
			}
			return nil
		}

		return event
	})
	form.SetRect(0, 0, 56, 16)
	if err := app.SetRoot(form, false).SetFocus(form).EnableMouse(true).Run(); err != nil {
		panic(err)
	}
}
Originally created by @elsni on GitHub (Nov 25, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/922 In a form, I want to move between FormItems using PgUp and PgDown in addition to Tab/Shift-Tab. So I implemented an InputCapture func to do this, but setFocus does not seem to do anything. Maybe I made a stupid mistake. used version: commit 7c9e464 code: ``` func TestForm() { app := tview.NewApplication() form := tview.NewForm(). AddInputField("Monday", "Test", 40, nil, nil). AddInputField("Tuesday", "Test", 40, nil, nil). AddInputField("Wednesday", "Test", 40, nil, nil). AddInputField("Thursday", "Test", 40, nil, nil) form.AddButton("Quit", func() { app.Stop() }) form.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyEscape: app.Stop() return nil case tcell.KeyPgDn: fallthrough case tcell.KeyPgUp: maxidx := form.GetFormItemCount() - 1 fidx, _ := form.GetFocusedItemIndex() // focused itemindex is -1 when a button is focused if fidx > -1 { if event.Key() == tcell.KeyPgDn { fidx += 1 if fidx > maxidx { fidx = 0 } } if event.Key() == tcell.KeyPgUp { fidx -= 1 if fidx < 0 { fidx = maxidx } } form.SetFocus(fidx) } return nil } return event }) form.SetRect(0, 0, 56, 16) if err := app.SetRoot(form, false).SetFocus(form).EnableMouse(true).Run(); err != nil { panic(err) } } ```
kerem closed this issue 2026-03-04 01:06:57 +03:00
Author
Owner

@rivo commented on GitHub (Nov 26, 2023):

Good catch. Thank you for letting me know. The latest commit should fix this.

<!-- gh-comment-id:1826772712 --> @rivo commented on GitHub (Nov 26, 2023): Good catch. Thank you for letting me know. The latest commit should fix this.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#673
No description provided.