How to get action name from event

Godot Version

4.2

Question

I trying to get action name from event that is triggered in c# .
i try to map between action name and event
i do able to get the event name , but i can’t find how to get action name that is associated.
To get the event name
String eventAsText =@event.AsText().ToLower();

Now i know all the actions are in InputMap .
but the keys are the actions not the events , so what is the fastest way to get the mapping
when i have the event name ?
or do i doing it all wrong ?
thanks

You can get from an InputEvent the names of all associated Actions in the following way using GDScript (the method can be adopted to C#):

func _input(event):
	var x: Array[StringName] = InputMap.get_actions()
	var r: Array[StringName]
	for a in x:
		if event.is_action(a):
			r.push_back(a)
	print ("Event '%s' has associated actions '%s'" % [event, r])
1 Like

An InputEvent can be multiple actions so you’ll need to filter them like:

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_type() and event.is_pressed():
		var actions = InputMap.get_actions().filter(func(action:StringName): return event.is_action(action, true))
		print('Event %s is in the following actions: %s' % [event, actions])

Whoops, just saw @Sauermann response :sweat_smile:

1 Like

but the actions list is very big as i see …
i guess i need to create static hash and initialize it lazy
thanks