[GH-ISSUE #181] SetInputCapture not working with forms #142

Closed
opened 2026-03-04 01:02:21 +03:00 by kerem · 4 comments
Owner

Originally created by @dentych on GitHub (Nov 7, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/181

Hello.

I don't seem to be able to make SetInputCapture work with forms.

I added a SetInputCapture to the bottom part of the Form primitive demo code.

// Demo code for the Form primitive.
package main

import (
	"fmt"
	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()
	form := tview.NewForm().
		AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil).
		AddInputField("First name", "", 20, nil, nil).
		AddInputField("Last name", "", 20, nil, nil).
		AddCheckbox("Age 18+", false, nil).
		AddPasswordField("Password", "", 10, '*', nil).
		AddButton("Save", nil).
		AddButton("Quit", func() {
			app.Stop()
		})
	form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)

        // ADDED THIS METHOD HERE
	form.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
		fmt.Println("TEST OUTPUT")
		return e
	})
        // END OF METHOD

	if err := app.SetRoot(form, true).Run(); err != nil {
		panic(err)
	}
}

"TEST OUTPUT" is never printed on the screen. It works fine with for example Lists

Originally created by @dentych on GitHub (Nov 7, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/181 Hello. I don't seem to be able to make SetInputCapture work with forms. I added a SetInputCapture to the bottom part of the Form primitive demo code. ```go // Demo code for the Form primitive. package main import ( "fmt" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() form := tview.NewForm(). AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil). AddInputField("First name", "", 20, nil, nil). AddInputField("Last name", "", 20, nil, nil). AddCheckbox("Age 18+", false, nil). AddPasswordField("Password", "", 10, '*', nil). AddButton("Save", nil). AddButton("Quit", func() { app.Stop() }) form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft) // ADDED THIS METHOD HERE form.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("TEST OUTPUT") return e }) // END OF METHOD if err := app.SetRoot(form, true).Run(); err != nil { panic(err) } } ``` "TEST OUTPUT" is never printed on the screen. It works fine with for example Lists
kerem closed this issue 2026-03-04 01:02:21 +03:00
Author
Owner

@rivo commented on GitHub (Nov 10, 2018):

Yes, that's because Form passes on the keyboard focus to its contained form elements. So it never receives any keyboard events itself.

You may be able to use Application.SetInputCapture() but it will capture all keyboard events, even if the form is currently not being used. So it will require some logic to determine if the user is currently working on the form.

Would that work for you?

<!-- gh-comment-id:437567735 --> @rivo commented on GitHub (Nov 10, 2018): Yes, that's because `Form` passes on the keyboard focus to its contained form elements. So it never receives any keyboard events itself. You may be able to use [`Application.SetInputCapture()`](https://godoc.org/github.com/rivo/tview#Application.SetInputCapture) but it will capture *all* keyboard events, even if the form is currently not being used. So it will require some logic to determine if the user is currently working on the form. Would that work for you?
Author
Owner

@rivo commented on GitHub (Nov 19, 2018):

I'm closing this for now. We can reopen this issue if you'd like to continue the discussion.

<!-- gh-comment-id:439834764 --> @rivo commented on GitHub (Nov 19, 2018): I'm closing this for now. We can reopen this issue if you'd like to continue the discussion.
Author
Owner

@dentych commented on GitHub (Nov 19, 2018):

Oh, sorry. I actually thought I had replied to this issue.

But it is possible to work around this, I just figured that because the method is available on the Form type it should also work as expected. Either that or have the method removed, so people don't get their hopes up 😄

<!-- gh-comment-id:439981313 --> @dentych commented on GitHub (Nov 19, 2018): Oh, sorry. I actually thought I had replied to this issue. But it is possible to work around this, I just figured that because the method is available on the Form type it should also work as expected. Either that or have the method removed, so people don't get their hopes up 😄
Author
Owner

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

I added some clarification to the function comments. I can't remove the function as it is inherited by Box.

<!-- gh-comment-id:441609586 --> @rivo commented on GitHub (Nov 26, 2018): I added some clarification to the function comments. I can't remove the function as it is inherited by `Box`.
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#142
No description provided.