How to make a custom event in code

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

I want to create an action say “move_up” when I press KEY_W but I don’t want to create it in the project settings is there any way to assign KEY_W to action “move_up” in gdscript?

:bust_in_silhouette: Reply From: Dlean Jeans

Try this:

# create the action 'move_up' if not already
if not InputMap.has_action('move_up'):
    InputMap.add_action('move_up')

var event = InputEventKey.new()
event.scancode = KEY_W
InputMap.action_add_event('move_up', event)

Check out this Input Mapping demo for more advanced key mapping.