[GH-ISSUE #38] Determine Primitive with the current focus? #29

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

Originally created by @triddell on GitHub (Jan 19, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/38

I'm using the app.SetInputCapture() function to setup some global hot-keys similar to the screens in your presentation sample. Instead of navigating back and forward though I have numbers as the hot-keys (1, 2, 3, etc.) for the various pages. But, when I have a form where I want to allow text to be entered, I need to shortcut the hot-keys and then allow numbers to be entered.

I have a hacky way of doing this now since all of my focused (and horizontal!) forms have the same (and unique in the app) "y" value as returned from this function:

_, y, _, _ := app.GetFocus().GetRect()

I'd like to do this more cleanly with the API though. Is there any way to determine the type of Primitive that has the current focus? I need to use some vertical forms in the future in different positions and this hack won't work then. If I know that a form has the current focus then I can shortcut the hot-key numbers and allow them to be entered into fields. I use a Cancel button to switch the focus off the form and then the hot-keys work again.

Thanks again for your hard work on tview! My app is progressing nicely and it is definitely fun to code!

Originally created by @triddell on GitHub (Jan 19, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/38 I'm using the app.SetInputCapture() function to setup some global hot-keys similar to the screens in your presentation sample. Instead of navigating back and forward though I have numbers as the hot-keys (1, 2, 3, etc.) for the various pages. But, when I have a form where I want to allow text to be entered, I need to shortcut the hot-keys and then allow numbers to be entered. I have a hacky way of doing this now since all of my focused (and horizontal!) forms have the same (and unique in the app) "y" value as returned from this function: `_, y, _, _ := app.GetFocus().GetRect()` I'd like to do this more cleanly with the API though. Is there any way to determine the type of Primitive that has the current focus? I need to use some vertical forms in the future in different positions and this hack won't work then. If I know that a form has the current focus then I can shortcut the hot-key numbers and allow them to be entered into fields. I use a Cancel button to switch the focus off the form and then the hot-keys work again. Thanks again for your hard work on tview! My app is progressing nicely and it is definitely fun to code!
kerem closed this issue 2026-03-04 01:01:15 +03:00
Author
Owner

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

I'm doing this in my own application:

app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
	if _, ok := app.GetFocus().(*tview.InputField); ok {
		return event
	}
  
	// Handle global keys.
	// ...
  
	return event
})

Basically, if an InputField has focus, I'm deactivating the global key handler. Would that help you in your case?

<!-- gh-comment-id:359243582 --> @rivo commented on GitHub (Jan 21, 2018): I'm doing this in my own application: ```go app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if _, ok := app.GetFocus().(*tview.InputField); ok { return event } // Handle global keys. // ... return event }) ``` Basically, if an `InputField` has focus, I'm deactivating the global key handler. Would that help you in your case?
Author
Owner

@triddell commented on GitHub (Jan 23, 2018):

Yes, this worked great for me too. Thanks!

<!-- gh-comment-id:359830752 --> @triddell commented on GitHub (Jan 23, 2018): Yes, this worked great for me too. Thanks!
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#29
No description provided.