[GH-ISSUE #794] [Question] Why SetInputCapture in for-loop not work #584

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

Originally created by @eeeXun on GitHub (Jan 16, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/794

Having two textarea, a and b. And I want to get their text when press <C-n>.
But it always get text of b, even I focused on a and press <C-n>

package main

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

func main() {
	flex := tview.NewFlex()
	app := tview.NewApplication()

	a := tview.NewTextArea()
	b := tview.NewTextArea()
	flex.AddItem(a, 0, 1, true).AddItem(b, 0, 1, true)

	for _, area := range []*tview.TextArea{a, b} {
		area.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
			key := event.Key()
			switch key {
			case tcell.KeyCtrlN:
				text := area.GetText()
				panic(text)
			}
			return event
		})
	}

	if err := app.SetRoot(flex, true).EnableMouse(true).Run(); err != nil {
		panic(err)
	}
}
Originally created by @eeeXun on GitHub (Jan 16, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/794 Having two textarea, `a` and `b`. And I want to get their text when press `<C-n>`. But it always get text of `b`, even I focused on `a` and press `<C-n>` ```go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { flex := tview.NewFlex() app := tview.NewApplication() a := tview.NewTextArea() b := tview.NewTextArea() flex.AddItem(a, 0, 1, true).AddItem(b, 0, 1, true) for _, area := range []*tview.TextArea{a, b} { area.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { key := event.Key() switch key { case tcell.KeyCtrlN: text := area.GetText() panic(text) } return event }) } if err := app.SetRoot(flex, true).EnableMouse(true).Run(); err != nil { panic(err) } } ```
kerem closed this issue 2026-03-04 01:06:14 +03:00
Author
Owner

@rivo commented on GitHub (Jan 16, 2023):

This is a Golang issue, not a tview issue. Check out https://github.com/golang/go/discussions/56010 for a discussion. Incidentally, this will be a breaking change in Golang, i.e. in some future Go version, your example will work as you expected it.

<!-- gh-comment-id:1383553814 --> @rivo commented on GitHub (Jan 16, 2023): This is a Golang issue, not a `tview` issue. Check out https://github.com/golang/go/discussions/56010 for a discussion. Incidentally, this will be a breaking change in Golang, i.e. in some future Go version, your example will work as you expected it.
Author
Owner

@eeeXun commented on GitHub (Jan 16, 2023):

Thanks @rivo for sharing this discussion!

<!-- gh-comment-id:1383614278 --> @eeeXun commented on GitHub (Jan 16, 2023): Thanks @rivo for sharing this discussion!
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#584
No description provided.