Skip to content

Commit

Permalink
Merge pull request #504 from gucio321/key-any
Browse files Browse the repository at this point in the history
input handler: add possibility to set additional input callback
  • Loading branch information
AllenDang authored May 9, 2022
2 parents e45585f + 195af50 commit 4014052
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
10 changes: 8 additions & 2 deletions InputHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ---
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions InputHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
8 changes: 8 additions & 0 deletions Keycode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
16 changes: 14 additions & 2 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
37 changes: 37 additions & 0 deletions examples/issue-501/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4014052

Please sign in to comment.