The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hi all,

As the title says, is there any way to modify the default behavior of focusing nodes using the keyboard?

Default implementation (without mouse):

  • uses ui_up, ui_down, ui_left and ui_right to navigate between neighbor control nodes
  • uses the computer's (Windows) repeat rate in keyboard properties (when you hold a ui_key while testing a godot game/app)

What I am trying to achieve:

  • remove the dependency on the computer keyboard's repeat rate property
  • set a specific focusing/navigation speed while holding down a key (ui_down, ui_up, ui_left and ui_right)

I've tried playing with func _input(event) and accept_event() but could not achieve what I was hoping for.
Any thought on this?
Any advice would be helpful.

Godot version v3.4.stable.official [206ba70f4]
in Engine by (95 points)

1 Answer

0 votes
Best answer

I did something like this recently, and I had a singleton node controlling things, and they had a variable that stored the currently focused Control, and an _input() function that checked if key was down, and then did whatever I needed...

If you add a variable that stores OS.get_system_time_msecs(), and each input you compare it against the current OS.get_system_time_msecs()then you can debounce the events and control how fast the repeat rate is.
Alternatively you can use the _physics_process(delta) event, and use the delta to control the repeat rate... you would still use Input.is_key_pressed()or Input.is_action_pressed()

by (1,346 points)
selected by

How would I debounce events?
Is there any other method other than accept_event()?

Have a var lastKeyTime = 0

On _input:
If abs(lastKeyTime - OS.get_system_time_msecs() ) < 100: Return lastKeyTime = OS.get_system_time_msecs()
Followed by whatever you actually want to happen. This will allow a max of one keypress per 100 milliseconds

Awesome!

I'm finally making progress here.
I ended up doing:
- accept_event() in func _input(event)
- lastkeytime in func _physics_process(delta) by using is_action_just_pressed()
- and if OS.get_system_time_msecs() - lastKeyTime > 100: by using is_action_pressed()

thanks a lot for your help

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.