Adding Input Actions in code


You need to add actions to the InputMap at runtime.


Typically, you’ll add actions to the InputMap via Project Settings, as shown in Recipe: Input Actions. However, you may find yourself needing to add one or more actions directly in a script. The InputMap singleton has methods to help you do this.

Here’s an example that would add a new action called “attack” using the space key:

func _ready():
    var ev =
    ev.keycode = KEY_SPACE
    InputMap.action_add_event("attack", ev)

If you also wanted to add the left mouse button to the same action:

ev =
ev.button_index = MOUSE_BUTTON_LEFT
InputMap.action_add_event("attack", ev)

InputMap.add_action() will produce an error if the action already exists. You should check first with InputMap.has_action() before attempting to add a new action.

Practical Example

Let’s say you’ve made the platform character from Recipe: Platform character and you want to re-use it in another project. If you saved the scene, script, and assets in a single folder, you need only copy that folder to your new project. But you’d still need to edit the Input Map in order for the inputs to work.

Instead, you could add the following code to the player script and be sure that the necessary input actions will be added automatically:

var controls = {"walk_right": [KEY_RIGHT, KEY_D],
                "walk_left": [KEY_LEFT, KEY_A],
                "jump": [KEY_UP, KEY_W, KEY_SPACE]}

func _ready():

func add_inputs():
    var ev
    for action in controls:
        if not InputMap.has_action(action):
        for key in controls[action]:
            ev =
            ev.keycode = key
            InputMap.action_add_event(action, ev)