[GH-ISSUE #39] SetDoneFunc doesn't seem to work when the inputField is inside a Form #32

Closed
opened 2026-03-04 01:01:16 +03:00 by kerem · 6 comments
Owner

Originally created by @radisb on GitHub (Jan 21, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/39

(Sorry if it is something very obvious, but I am very new to golang so maybe I miss something. )

package main

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

func main() {
	app := tview.NewApplication()
	field := tview.NewInputField().SetDoneFunc(func (key tcell.Key) { app.Stop() })

	//container := tview.NewForm().AddFormItem(field)
	// OR
        //container := tview.NewFlex().AddItem(field, 0, 1, true)

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

In the code above, when I press Enter, the app stops only with the second container line (inside a Flex). If I use the first container (the Form) nothing is happening. Is this by design?

Originally created by @radisb on GitHub (Jan 21, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/39 (Sorry if it is something very obvious, but I am very new to golang so maybe I miss something. ) ``` package main import ( "github.com/rivo/tview" "github.com/gdamore/tcell" ) func main() { app := tview.NewApplication() field := tview.NewInputField().SetDoneFunc(func (key tcell.Key) { app.Stop() }) //container := tview.NewForm().AddFormItem(field) // OR //container := tview.NewFlex().AddItem(field, 0, 1, true) if err := app.SetRoot(container, true).SetFocus(field).Run(); err != nil { panic(err) } } ``` In the code above, when I press Enter, the app stops only with the second container line (inside a Flex). If I use the first container (the Form) nothing is happening. Is this by design?
kerem closed this issue 2026-03-04 01:01:16 +03:00
Author
Owner

@rivo commented on GitHub (Jan 21, 2018):

Actually, this is by design. Check out the documentation for AddFormItem():

Note, however, that the Form class will override some of its attributes to make it work in the form context.

I don't specify exactly what's being overridden because it's different depending on the type of form element. But the done handler is definitely overridden by the Form class (your own handler will be dropped).

The reason for this is that if such a field is embedded in a form, the desired behaviour is to move on to the next field when the user presses Enter (or Tab). If you insist on quitting the application (or anything else) when user presses Enter, you can use SetInputCapture() on your input field to override key events.

<!-- gh-comment-id:359275972 --> @rivo commented on GitHub (Jan 21, 2018): Actually, this is by design. Check out the [documentation for `AddFormItem()`](https://godoc.org/github.com/rivo/tview#Form.AddFormItem): > Note, however, that the Form class will override some of its attributes to make it work in the form context. I don't specify exactly what's being overridden because it's different depending on the type of form element. But the `done` handler is definitely overridden by the `Form` class (your own handler will be dropped). The reason for this is that if such a field is embedded in a form, the desired behaviour is to move on to the next field when the user presses Enter (or Tab). If you insist on quitting the application (or anything else) when user presses Enter, you can use [`SetInputCapture()`](https://godoc.org/github.com/rivo/tview#Box.SetInputCapture) on your input field to override key events.
Author
Owner

@radisb commented on GitHub (Jan 22, 2018):

Got it. Thanks for your answer.

On Sunday, January 21, 2018, rivo notifications@github.com wrote:

Actually, this is by design. Check out the documentation for AddFormItem()
https://godoc.org/github.com/rivo/tview#Form.AddFormItem:

Note, however, that the Form class will override some of its attributes to
make it work in the form context.

I don't specify exactly what's being overridden because it's different
depending on the type of form element. But the done handler is definitely
overridden by the Form class (your own handler will be dropped).

The reason for this is that if such a field is embedded in a form, the
desired behaviour is to move on to the next field when the user presses
Enter (or Tab). If you insist on quitting the application (or anything
else) when user presses Enter, you can use SetInputCapture()
https://godoc.org/github.com/rivo/tview#Box.SetInputCapture on your
input field to override key events.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rivo/tview/issues/39#issuecomment-359275972, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABU66pbf50NmdH7fgB_vG0c8ureyPZMAks5tM5dTgaJpZM4Rl123
.

<!-- gh-comment-id:359359743 --> @radisb commented on GitHub (Jan 22, 2018): Got it. Thanks for your answer. On Sunday, January 21, 2018, rivo <notifications@github.com> wrote: > Actually, this is by design. Check out the documentation for AddFormItem() > <https://godoc.org/github.com/rivo/tview#Form.AddFormItem>: > > Note, however, that the Form class will override some of its attributes to > make it work in the form context. > > I don't specify exactly what's being overridden because it's different > depending on the type of form element. But the done handler is definitely > overridden by the Form class (your own handler will be dropped). > > The reason for this is that if such a field is embedded in a form, the > desired behaviour is to move on to the next field when the user presses > Enter (or Tab). If you insist on quitting the application (or anything > else) when user presses Enter, you can use SetInputCapture() > <https://godoc.org/github.com/rivo/tview#Box.SetInputCapture> on your > input field to override key events. > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/rivo/tview/issues/39#issuecomment-359275972>, or mute > the thread > <https://github.com/notifications/unsubscribe-auth/ABU66pbf50NmdH7fgB_vG0c8ureyPZMAks5tM5dTgaJpZM4Rl123> > . >
Author
Owner

@joegrasse commented on GitHub (Apr 18, 2018):

@rivo I just discovered this as well. It would be nice if the Docs for SetDoneFunc on the form items called this out as well.

<!-- gh-comment-id:382407921 --> @joegrasse commented on GitHub (Apr 18, 2018): @rivo I just discovered this as well. It would be nice if the Docs for SetDoneFunc on the form items called this out as well.
Author
Owner

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

FYI, I've separated the "done" function from the "finished" function so if your primitive is part of a form, your own "done" callback won't be overwritten anymore.

<!-- gh-comment-id:382855450 --> @rivo commented on GitHub (Apr 19, 2018): FYI, I've separated the "done" function from the "finished" function so if your primitive is part of a form, your own "done" callback won't be overwritten anymore.
Author
Owner

@felipeluna commented on GitHub (Feb 25, 2019):

Hi, If I have this

inputerField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyTAB {
			app.SetFocus(textView)
		}
		return event
	})

the behaviour is to go to the next item in the form, not to set focus where i want. Is there any reason for this? How can i override the "Done" keys in a input field?

In my application i have several forms, ( because i couldn't figure out how to organize the input fields the way i want)

I like the cycle behaviour, but i would like to go to the next form when I'm done with the latest inputfield.

Thanks

<!-- gh-comment-id:466954469 --> @felipeluna commented on GitHub (Feb 25, 2019): Hi, If I have this ```golang inputerField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyTAB { app.SetFocus(textView) } return event }) ``` the behaviour is to go to the next item in the form, not to set focus where i want. Is there any reason for this? How can i override the "**Done**" keys in a input field? In my application i have several forms, ( because i couldn't figure out how to organize the input fields the way i want) I like the cycle behaviour, but i would like to go to the next form when I'm done with the latest inputfield. Thanks
Author
Owner

@rivo commented on GitHub (Mar 9, 2019):

Have you tried returning nil from within your if statement?

<!-- gh-comment-id:471174584 --> @rivo commented on GitHub (Mar 9, 2019): Have you tried returning `nil` from within your `if` statement?
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#32
No description provided.