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

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

in Engine by (40 points)

3 Answers

0 votes

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.

by (1,422 points)

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.

+1 vote

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)
by (40 points)
edited by
0 votes

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

by (26 points)
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.