Check if an arbitrary input action is pressed

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

Hey guys, loving Godot so far but got stumped. So far I only know of ways to check if a player input is mapped to a specific action.
Is there a way to check if a player input is mapped to any action in general and not just one?

:bust_in_silhouette: Reply From: Eric Ellingson

You can probably just set up a mapping yourself. Try maybe something like this:

var map = {}

func _ready():
	set_up_action_map()

func set_up_action_map():
	for action in InputMap.get_actions():
		for input in InputMap.get_action_list(action):
			if input is InputEventKey:
				map[input.scancode] = action
	print(map)

This will give you something like:

{
    <scancode_1>: 'ui_down',
    <scancode_2>: 'ui_left',
}

where <scancode_*> is the scancode for the key press input.

then, you can just check if a scancode exists in the map to see if that key is assigned to an action.

Works just as you said, thanks!

chaunce | 2019-10-25 02:07