diff --git a/InputHandler.go b/InputHandler.go index f951f773..c9172971 100644 --- a/InputHandler.go +++ b/InputHandler.go @@ -29,6 +29,8 @@ const ( LocalShortcut ShortcutType = false ) +type InputHandlerHandleCallback func(Key, Modifier, Action) + // InputHandler is an interface which needs to be implemented // by user-definied input handlers. type InputHandler interface { @@ -37,7 +39,7 @@ type InputHandler interface { // UnregisterKeyboardShortcuts removes iwndow shourtcuts from input handler UnregisterWindowShortcuts() // Handle handles a shortcut - Handle(Key, Modifier) + Handle(Key, Modifier, Action) } // --- Default implementation of giu input manager --- @@ -79,7 +81,11 @@ func (i *inputHandler) UnregisterWindowShortcuts() { } } -func (i *inputHandler) Handle(key Key, mod Modifier) { +func (i *inputHandler) Handle(key Key, mod Modifier, a Action) { + if a != Press { + return + } + for combo, cb := range i.shortcuts { if combo.key != key || combo.modifier != mod { continue diff --git a/InputHandler_test.go b/InputHandler_test.go index 544aedd9..87561cb3 100644 --- a/InputHandler_test.go +++ b/InputHandler_test.go @@ -86,13 +86,13 @@ func Test_InputHandler_Handle(t *testing.T) { i.RegisterKeyboardShortcuts(sh...) - i.Handle(Key(0), Modifier(0)) + i.Handle(Key(0), Modifier(0), Press) a.False(shortcut1, "Shortcut 1 was handled, but shouldn't.") a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.") - i.Handle(Key(5), Modifier(0)) + i.Handle(Key(5), Modifier(0), Press) a.True(shortcut1, "Shortcut 1 was not handled, but shouldn be.") a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.") - i.Handle(Key(8), Modifier(2)) + i.Handle(Key(8), Modifier(2), Press) a.True(shortcut1, "Shortcut 1 was not handled, but shouldn be.") a.True(shortcut2, "Shortcut 2 was not handled, but shouldn be.") } diff --git a/Keycode.go b/Keycode.go index e54a125c..a861814f 100644 --- a/Keycode.go +++ b/Keycode.go @@ -146,3 +146,11 @@ const ( ModCapsLock Modifier = Modifier(glfw.ModCapsLock) ModNumLock Modifier = Modifier(glfw.ModNumLock) ) + +type Action glfw.Action + +const ( + Release Action = Action(glfw.Release) + Press Action = Action(glfw.Press) + Repeat Action = Action(glfw.Repeat) +) diff --git a/MasterWindow.go b/MasterWindow.go index 5a798b77..ca2ddb9b 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -44,6 +44,10 @@ type MasterWindow struct { context *imgui.Context io *imgui.IO updateFunc func() + + // possibility to expend InputHandler's stuff + // See SetAdditionalInputHandler + additionalInputCallback InputHandlerHandleCallback } // NewMasterWindow creates a new master window and initializes GLFW. @@ -392,8 +396,16 @@ func (w *MasterWindow) SetShouldClose(v bool) { func (w *MasterWindow) SetInputHandler(handler InputHandler) { Context.InputHandler = handler w.platform.SetInputCallback(func(key glfw.Key, modifier glfw.ModifierKey, action glfw.Action) { - if action == glfw.Press { - handler.Handle(Key(key), Modifier(modifier)) + k, m, a := Key(key), Modifier(modifier), Action(action) + handler.Handle(k, m, a) + if w.additionalInputCallback != nil { + w.additionalInputCallback(k, m, a) } }) } + +// SetAdditionalInputHandlerCallback allows to set an input callback to handle more events (not only these from giu.inputHandler). +// See examples/issue-501. +func (w *MasterWindow) SetAdditionalInputHandlerCallback(cb InputHandlerHandleCallback) { + w.additionalInputCallback = cb +} diff --git a/examples/issue-501/main.go b/examples/issue-501/main.go new file mode 100644 index 00000000..a9ff8412 --- /dev/null +++ b/examples/issue-501/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/AllenDang/giu" +) + +var ( + command string + shouldFocus bool +) + +func loop() { + giu.SingleWindow().Layout( + giu.Custom(func() { + if shouldFocus { + shouldFocus = false + giu.SetKeyboardFocusHere() + } + }), + giu.Row( + giu.InputText(&command).Size(200), + giu.Label("<- press any key to start typing here!"), + ), + ) +} + +func onAnyKeyPressed(key giu.Key, mod giu.Modifier, action giu.Action) { + if action == giu.Press { + shouldFocus = true + } +} + +func main() { + wnd := giu.NewMasterWindow("Handle any key event", 640, 480, 0) + wnd.SetAdditionalInputHandlerCallback(onAnyKeyPressed) + wnd.Run(loop) +}