How to change InputMap in the editor from script?

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

I created this EditorScript file that should reset the InputMap to the default actions and events, then ran it but it hasn’t changed the InputMap at all. How should I achieve this?

tool
extends EditorScript

const INPUT_ACTIONS = ["ui_accept", "ui_select", "ui_cancel", "ui_focus_next", "ui_focus_prev", "ui_left", "ui_right", "ui_up", "ui_down", "ui_page_up", "ui_page_down", "ui_home", "ui_end"]

func _run():
    # Erase all actions in InputMap.
    for input_action in InputMap.get_actions():
        InputMap.erase_action(input_action)

    # Add all the actions defined in INPUT_ACTIONS.
    for input_action in INPUT_ACTIONS:
        InputMap.add_action(input_action)
        
    var current_action: String = ""
    var input_event_key = InputEventKey.new()
    var input_event_joypad_button = InputEventJoypadButton.new()
    
    # ui_accept
    current_action = "ui_accept"
    input_event_key.set_scancode(KEY_ENTER)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_key.set_scancode(KEY_KP_ENTER)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_key.set_scancode(KEY_SPACE)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_0)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_select
    current_action = "ui_select"
    input_event_key.set_scancode(KEY_SPACE)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_3)
    InputMap.action_add_event(current_action, input_event_joypad_button)

    # ui_cancel
    current_action = "ui_cancel"
    input_event_key.set_scancode(KEY_ESCAPE)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_1)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_focus_next
    current_action = "ui_focus_next"
    input_event_key.set_scancode(KEY_TAB)
    InputMap.action_add_event(current_action, input_event_key)
    
    # ui_focus_prev
    current_action = "ui_focus_prev"
    input_event_key.set_scancode(KEY_TAB)
    input_event_key.set_shift(true)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_key.set_shift(false)
    
    # ui_left
    current_action = "ui_left"
    input_event_key.set_scancode(KEY_LEFT)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_14)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_right
    current_action = "ui_right"
    input_event_key.set_scancode(KEY_RIGHT)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_15)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_up
    current_action = "ui_up"
    input_event_key.set_scancode(KEY_UP)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_12)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_down
    current_action = "ui_down"
    input_event_key.set_scancode(KEY_DOWN)
    InputMap.action_add_event(current_action, input_event_key)
    input_event_joypad_button.set_button_index(JOY_BUTTON_13)
    InputMap.action_add_event(current_action, input_event_joypad_button)
    
    # ui_page_up
    current_action = "ui_page_up"
    input_event_key.set_scancode(KEY_PAGEUP)
    InputMap.action_add_event(current_action, input_event_key)
    
    # ui_page_down
    current_action = "ui_page_down"
    input_event_key.set_scancode(KEY_PAGEDOWN)
    InputMap.action_add_event(current_action, input_event_key)

    # ui_home
    current_action = "ui_home"
    input_event_key.set_scancode(KEY_HOME)
    InputMap.action_add_event(current_action, input_event_key)

    # ui_end
    current_action = "ui_end"
    input_event_key.set_scancode(KEY_END)
    InputMap.action_add_event(current_action, input_event_key)