mirror of
https://github.com/rivo/tview.git
synced 2026-04-27 05:45:49 +03:00
[GH-ISSUE #409] Application.SetInputCapture doesn't seem to capture anything #300
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/tview#300
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Abenstex on GitHub (Feb 18, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/409
I setup an application with pages. When the user presses a key combination the pages should be changed. However, trying to capture the key events does not appear to do anything:
`app := tview.NewApplication()
When I call
mainGrid.Pages.SwitchToPage("Welcome")without waiting for key presses then the correct page is shown, though. To debug I just wanted to print something to a Textview, but that isn't working either. So my question is what am I missing or doing wrong?@tslocum commented on GitHub (Feb 18, 2020):
Hey @Abenstex. Did you read the relevant documentation before submitting this? It states:
TextView is a box which displays text. It implements the io.Writer interface so you can stream text to it. This does not trigger a redraw automatically but if a handler is installed via SetChangedFunc(), you can cause it to be redrawn.Setting a handler via SetChangedFunc which calls Application.Draw whenever the TextView is changed will resolve this. You could also make any text changes first and then manually trigger an application draw by calling Application.QueueUpdateDraw with an empty function.
@Abenstex commented on GitHub (Feb 19, 2020):
@tslocum thanks for your reply. As a matter of fact I hadn't read that part or didn't remember it. Anyway, what I did then was to include the following lines
app.QueueUpdateDraw(func() { app.SetRoot(mainGrid.Grid, true) }) mainGrid.InfoBar.SetChangedFunc(func() { app.Draw() })First I only called
app.QueueUpdateDrawbut since that didn't help I addedmainGrid.InfoBar.SetChangedFunc. Unfortunately it still isn't captchuring any key events at least not visibly.@Abenstex commented on GitHub (Feb 19, 2020):
As a matter of fact I included
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { panic(errors.New("Key event: "+event.Name())) ...but the program doesn't even panic. So somehow I'm missing something essential@rivo commented on GitHub (Feb 19, 2020):
You're creating a variable
appsomewhere to whichSetInputCapture()is attached. But you're not using it to run the application. You actually create anotherApplication(withtview.NewApplication()) object when you start your session. That's whySetInputCapture()has no effect.Here's an example that should work:
You also don't need to call
app.Draw()as @tslocum suggested because the handler provided with theSetInputCapture()function will always be called as part of the main goroutine and a call toapp.Draw()will follow implicitly. See here for more information.@Abenstex commented on GitHub (Feb 19, 2020):
Oh damn...sorry I really didn't see that. Sometimes you just don't see the forest for the trees.