[GH-ISSUE #465] Nil InputHandler doesn't skip custom form element #336

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

Originally created by @tsak on GitHub (Jul 7, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/465

Hiya,

@rivo I absolutely love your library. Building command line interfaces with it is quite fun.

I'm currently building an interface for a user shell replacement, part of which is a form for network settings.

In order to make it more usable, I added a custom form element FormHeader (see this gist for code).

I'm using the following InputHandler, naively assuming that by using WrapInputHandler(nil) a FormHeader would be skipped when tabbing through the form.

func (f *FormHeader) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
	return f.WrapInputHandler(nil)
}

Unfortunately, it doesn't and I'm wondering if there was a better way to put skipable elements into a form. This touches lightly on #192 as I am also planning to disable parts of the form if DHCP is enabled, but still want to show a network interface's address.

image

Thanks,
tsak

Originally created by @tsak on GitHub (Jul 7, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/465 Hiya, @rivo I absolutely love your library. Building command line interfaces with it is quite fun. I'm currently building an interface for a user shell replacement, part of which is a form for network settings. In order to make it more usable, I added a custom form element `FormHeader` (see [this gist](https://gist.github.com/tsak/49e54b7122409f289fb6d10cd19cfda1) for code). I'm using the following `InputHandler`, naively assuming that by using `WrapInputHandler(nil)` a `FormHeader` would be skipped when tabbing through the form. ```go func (f *FormHeader) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { return f.WrapInputHandler(nil) } ``` Unfortunately, it doesn't and I'm wondering if there was a better way to put skipable elements into a form. This touches lightly on #192 as I am also planning to disable parts of the form if DHCP is enabled, but still want to show a network interface's address. ![image](https://user-images.githubusercontent.com/1022521/86788991-b5eeb400-c05e-11ea-9526-e96f39a01e32.png) Thanks, tsak
kerem closed this issue 2026-03-04 01:04:07 +03:00
Author
Owner

@gnojus commented on GitHub (Jul 9, 2020):

Hi,

if you don't want to handle input, you shouldn't need an input handler at all. Instead I would recommend to instantly pass to next form item when FormHeader receives focus. Something like this:

func (f *FormHeader) Focus(func(tview.Primitive)) {
	if f.finished != nil {
		f.finished(tcell.KeyTAB)
	}
}
<!-- gh-comment-id:656143149 --> @gnojus commented on GitHub (Jul 9, 2020): Hi, if you don't want to handle input, you shouldn't need an input handler at all. Instead I would recommend to instantly pass to next form item when `FormHeader` receives focus. Something like this: ```go func (f *FormHeader) Focus(func(tview.Primitive)) { if f.finished != nil { f.finished(tcell.KeyTAB) } } ```
Author
Owner

@gnojus commented on GitHub (Jul 9, 2020):

I realized that my suggested fix will only pass focus downwards, even when pressing backtab on the form. With the current implementation of tview I can't come up with a nice solution, because the key events cannot be intercepted higher up than the form item, only at application level. The only way to solve this that comes to my mind is to catch the tabs/backtabs on the app level and inform all the FormHeader's to act accordingly in case they receive focus.

<!-- gh-comment-id:656170633 --> @gnojus commented on GitHub (Jul 9, 2020): I realized that my suggested fix will only pass focus downwards, even when pressing backtab on the form. With the current implementation of `tview` I can't come up with a nice solution, because the key events cannot be intercepted higher up than the form item, only at application level. The only way to solve this that comes to my mind is to catch the tabs/backtabs on the app level and inform all the `FormHeader`'s to act accordingly in case they receive focus.
Author
Owner

@tsak commented on GitHub (Jul 9, 2020):

@nojus297 thank you so much for your great suggestion. I might be able to hack around the SHIFT+TAB situation by tracking which field had focus before and store pointers in the FormHeader itself.

Thanks again for your help. It pointed me the right direction.

<!-- gh-comment-id:656360817 --> @tsak commented on GitHub (Jul 9, 2020): @nojus297 thank you so much for your great suggestion. I might be able to hack around the `SHIFT+TAB` situation by tracking which field had focus before and store pointers in the `FormHeader` itself. Thanks again for your help. It pointed me the right direction.
Author
Owner

@rivo commented on GitHub (Jul 12, 2020):

the key events cannot be intercepted higher up than the form item, only at application level

I have planned to change this behaviour. See #421. Hopefully soon.

<!-- gh-comment-id:657231624 --> @rivo commented on GitHub (Jul 12, 2020): > the key events cannot be intercepted higher up than the form item, only at application level I have planned to change this behaviour. See #421. Hopefully soon.
Author
Owner

@gnojus commented on GitHub (Jul 12, 2020):

I have planned to change this behaviour. See #421. Hopefully soon.

Just to add to this, on my fork I have changed this behavior so that all child-parent line of primitives are considered to have focus, but application knows only about the top one and passes key events to that. The primitives are responsible to pass down the key events (or intercept) to their focused children. This was done before mouse was introduced to tview though.

<!-- gh-comment-id:657249651 --> @gnojus commented on GitHub (Jul 12, 2020): > I have planned to change this behaviour. See #421. Hopefully soon. Just to add to this, on my fork I have changed this behavior so that all child-parent line of primitives are considered to have focus, but application knows only about the top one and passes key events to that. The primitives are responsible to pass down the key events (or intercept) to their focused children. This was done before mouse was introduced to `tview` though.
Author
Owner

@rivo commented on GitHub (Aug 18, 2020):

Just as a heads up, keyboard events are now propagated down the hierarchy. They can be intercepted somewhere along the line.

<!-- gh-comment-id:675390082 --> @rivo commented on GitHub (Aug 18, 2020): Just as a heads up, keyboard events are now propagated down the hierarchy. They can be intercepted somewhere along the line.
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#336
No description provided.