Is there a way to modify focusing of control nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By haha-lolcat1

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.

:bust_in_silhouette: Reply From: rossunger

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()

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

haha-lolcat1 | 2022-02-10 04:14

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

rossunger | 2022-02-10 04:58


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

haha-lolcat1 | 2022-02-11 15:05