Provided a way to forward Ctrl-C to primites. Resolves #816

This commit is contained in:
Oliver 2023-02-26 20:52:29 +01:00
parent c8e57d57c6
commit 47e7db7885
1 changed files with 12 additions and 4 deletions

View File

@ -135,9 +135,16 @@ func NewApplication() *Application {
// different one) by returning it or stop the key event processing by returning // different one) by returning it or stop the key event processing by returning
// nil. // nil.
// //
// Note that this also affects the default event handling of the application // The only default global key event is Ctrl-C which stops the application. It
// itself: Such a handler can intercept the Ctrl-C event which closes the // requires special handling:
// application. //
// - If you do not wish to change the default behavior, return the original
// event object passed to your input capture function.
// - If you wish to block Ctrl-C from any functionality, return nil.
// - If you do not wish Ctrl-C to stop the application but still want to
// forward the Ctrl-C event to primitives down the hierarchy, return a new
// key event with the same key and modifiers, e.g.
// tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone).
func (a *Application) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Application { func (a *Application) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Application {
a.inputCapture = capture a.inputCapture = capture
return a return a
@ -315,6 +322,7 @@ EventLoop:
// Intercept keys. // Intercept keys.
var draw bool var draw bool
originalEvent := event
if inputCapture != nil { if inputCapture != nil {
event = inputCapture(event) event = inputCapture(event)
if event == nil { if event == nil {
@ -325,7 +333,7 @@ EventLoop:
} }
// Ctrl-C closes the application. // Ctrl-C closes the application.
if event.Key() == tcell.KeyCtrlC { if event == originalEvent && event.Key() == tcell.KeyCtrlC {
a.Stop() a.Stop()
break break
} }