How to add an InputEvent to InputMap with code?

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

How can I edit my InputMap with code? I cannot see any examples in the doc.Or there is some examples which I missed?

:bust_in_silhouette: Reply From: Xrayez

Never used it but I think it’ll go like this:

func _ready():
    InputMap.add_action("my_action")
    InputMap.action_add_event("my_action", event)

See InputMap singleton documentation for other methods on how to modify input map.

Thank you. But what I confused in is how to set an InputEvent binding a key like “KEY_A” so that when I press the “A” key it can feedback the action Input.is_action_pressed("A").
Here is the Input singleton of the document:

var ev = InputEventAction.new()
ev.set_as_action("move_left", true)
Input.parse_input_event(ev)

Still not understand how to do it.

DogeMassaji | 2018-07-10 15:55

1 Like
:bust_in_silhouette: Reply From: DogeMassaji

OK. I got the answer in the forum.

var ev = InputEventKey.new()
ev.scancode = KEY_A
InputMap.add_action("ui_a")
InputMap.action_add_event("ui_a", ev)
:bust_in_silhouette: Reply From: BananaBread

Working on a plugin in Godot 4.0.1 so here is an update:

var key = InputEventKey.new()
key.physical_keycode = KEY_W
	
InputMap.add_action("__custom_action")
InputMap.action_add_event("__custom_action", key)

In my editor plugin context it’s also advisable to clean up the action. But that’s probably not the case for a game.

InputMap.erase_action("__custom_action")