From 93ef65672e11a331d493b2d325ea236ad07ea204 Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Tue, 23 Jan 2018 00:06:40 -0700 Subject: [PATCH] Capture all events in App --- application.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/application.go b/application.go index 5693d16..2b3f210 100644 --- a/application.go +++ b/application.go @@ -29,7 +29,7 @@ type Application struct { // An optional capture function which receives a key event and returns the // event to be forwarded to the default input handler (nil if nothing should // be forwarded). - inputCapture func(event *tcell.EventKey) *tcell.EventKey + inputCapture func(event tcell.Event) tcell.Event } // NewApplication creates and returns a new application. @@ -46,11 +46,15 @@ func NewApplication() *Application { // Note that this also affects the default event handling of the application // itself: Such a handler can intercept the Ctrl-C event which closes the // applicatoon. -func (a *Application) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Application { +func (a *Application) SetInputCapture(capture func(event tcell.Event) tcell.Event) *Application { a.inputCapture = capture return a } +func (a *Application) Screen() tcell.Screen { + return a.screen +} + // Run starts the application and thus the event loop. This function returns // when Stop() was called. func (a *Application) Run() error { @@ -97,20 +101,20 @@ func (a *Application) Run() error { break // The screen was finalized. } + // Intercept all events. + if a.inputCapture != nil { + event = a.inputCapture(event) + if event == nil { + break // Don't forward event. + } + } + switch event := event.(type) { case *tcell.EventKey: a.RLock() p := a.focus a.RUnlock() - // Intercept keys. - if a.inputCapture != nil { - event = a.inputCapture(event) - if event == nil { - break // Don't forward event. - } - } - // Ctrl-C closes the application. if event.Key() == tcell.KeyCtrlC { a.Stop()