How to intercept key input

Godot Version

4.2.2

Question

I made a function to modify the key bindings.

extends Button

@export var action_name: String = ""

func _ready():
	set_process_input(false)
	if InputMap.has_action(action_name):
		var events: Array = InputMap.action_get_events(action_name)
		var display_str: String = ""
		for e in events:
			display_str += e.as_text()
		set_text("[%s] %s" % [display_str, action_name])

func _on_pressed():
	set_text("[Please Input New Key...] %s" % action_name)
	set_process_input(true)

func _input(event):
	if KeyBinding.set_key_binding(action_name, event):
		set_text("[%s] %s" % [InputMap.action_get_events(action_name)[0].as_text(), action_name])
		set_process_input(false)

By clicking this button, it will try to capture the new input and then bind it to the specified action.



But if I enter another action that already exists, it will continue to trigger after the binding is complete, e.g., ui_down, which focus the next option after the binding is completed.
screenshoot2
In my expectation, this input should only trigger the “bind” action. What method should I use to discard this event after _input is active.

Viewport.set_input_as_handled()

1 Like