Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Global key press event #501

Closed
aj3423 opened this issue Apr 22, 2022 · 4 comments
Closed

[Feature Request] Global key press event #501

aj3423 opened this issue Apr 22, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@aj3423
Copy link

aj3423 commented Apr 22, 2022

Related problem

I'm writing a debugger, there is an giu.InputText for typing commands. It isn't always focused because user may click somewhere else, I want to auto-focus the input when user start typing commands, instead of clicking it to gain focus manually.

I see giu.Event().OnKeyPressed() and giu.IsKeyPressed(), they both require a specific key code. Possible to monitor key event globally?

Your request

.

Alternative solution

No response

Additional context

No response

@aj3423 aj3423 added the enhancement New feature or request label Apr 22, 2022
@gucio321
Copy link
Collaborator

Thats only my suppose:
maybe Dear ImGUI will handle something like:

KeyAny = KayA| KeyB | KeyC // etc

@gucio321
Copy link
Collaborator

gucio321 commented May 7, 2022

well, @aj3423 I've solved this. please take a look on #504

@aj3423
Copy link
Author

aj3423 commented May 8, 2022

@gucio321 Thanks. it still need some "adjustment", but works now.

package main

import (
	"fmt"
	"strings"

	g "github.com/AllenDang/giu"
)

var input string

var focused bool
var should_focus bool

func loop() {
	g.SingleWindow().Layout(
		g.Custom(func() {
			if should_focus {
				should_focus = true
				focused = true
				g.SetKeyboardFocusHere()
			}
			g.InputText(&input).Build()
		}),
	)
}

func main() {
	wnd := g.NewMasterWindow("", 400, 300, 0)

	wnd.SetAdditionalInputHandlerCallback(func(key g.Key, mod g.Modifier, act g.Action) {
		if !focused && act == g.Press {
			input += strings.ToLower(string([]byte{byte(key)}))
			should_focus = true
		}
		fmt.Println(key, mod, act)
	})
	wnd.Run(loop)
}

@aj3423 aj3423 closed this as completed May 8, 2022
@gucio321
Copy link
Collaborator

gucio321 commented May 8, 2022

@aj3423 I did an example for that issue here: https://github.com/AllenDang/giu/blob/9e3f60abdd963f8e7da62133b2d9a2b3c1459356/examples/issue-501/main.go it should be what you're looking for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants