[GH-ISSUE #527] Treeview: Call a function for each node traversed with keyboard keys? #382

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

Originally created by @telefax on GitHub (Nov 13, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/527

Is there a way to call a function each time I traverse a node?
For instance, I would like to automatically send my node reference to a textview in another grid column while I'm running over the nodes with arrow keys.

Right now I have only figured out how to send this text when pressing the enter key (selectedfunc), but I want to do this without having to press any additional key, just by landing on a node without pressing enter.

Any ideas?

Thanks!

Originally created by @telefax on GitHub (Nov 13, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/527 Is there a way to call a function each time I traverse a node? For instance, I would like to automatically send my node reference to a textview in another grid column while I'm running over the nodes with arrow keys. Right now I have only figured out how to send this text when pressing the enter key (selectedfunc), but I want to do this without having to press any additional key, just by landing on a node without pressing enter. Any ideas? Thanks!
kerem closed this issue 2026-03-04 01:04:31 +03:00
Author
Owner

@telefax commented on GitHub (Nov 14, 2020):

I poked around and came up with this:

app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		ref := tree.GetCurrentNode().GetReference()
		textView.Clear()
		switch event.Key() {
		case tcell.KeyDown:

			if ref != nil {
				fmt.Fprintf(textView, "%s ", ref)
			}
		case tcell.KeyUp:
			if ref != nil {
				fmt.Fprintf(textView, "%s ", ref)
			}
		}
		return event
	})

First I need to capture all in built keyboard shortcuts that you can navigate with on the tree, but the biggest problem is that..SetInputCapture plays out before the node is actually traversed, so this gives me the reference of the previous node and not the current one.

Any other clever way to solve this?

<!-- gh-comment-id:727253926 --> @telefax commented on GitHub (Nov 14, 2020): I poked around and came up with this: ``` app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { ref := tree.GetCurrentNode().GetReference() textView.Clear() switch event.Key() { case tcell.KeyDown: if ref != nil { fmt.Fprintf(textView, "%s ", ref) } case tcell.KeyUp: if ref != nil { fmt.Fprintf(textView, "%s ", ref) } } return event }) ``` First I need to capture all in built keyboard shortcuts that you can navigate with on the tree, but the biggest problem is that..SetInputCapture plays out before the node is actually traversed, so this gives me the reference of the previous node and not the current one. Any other clever way to solve this?
Author
Owner

@rlmaers commented on GitHub (Nov 25, 2020):

Isn't TreeView.SetChangedFunc exactly what you're looking for?

<!-- gh-comment-id:733528363 --> @rlmaers commented on GitHub (Nov 25, 2020): Isn't [TreeView.SetChangedFunc](https://pkg.go.dev/github.com/rivo/tview#TreeView.SetChangedFunc) exactly what you're looking for?
Author
Owner

@telefax commented on GitHub (Nov 25, 2020):

Pretty impressive how I could miss this! Thanks!

<!-- gh-comment-id:733963376 --> @telefax commented on GitHub (Nov 25, 2020): Pretty impressive how I could miss this! Thanks!
Author
Owner

@telefax commented on GitHub (Nov 25, 2020):

Solved.

<!-- gh-comment-id:733963423 --> @telefax commented on GitHub (Nov 25, 2020): Solved.
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#382
No description provided.