Get InputMap as readable text

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

Does GDScript have a simple function or command that can display a readable version of whatever key/mouse button is mapped to an InputMap? For example, “ui_up” has ‘Up’ and a Joypad button assigned to it, and using InputMap.get_action_list("ui_up") gives me: [[InputEventKey:26], [InputEventJoypadButton:27]]

If I displayed that in a controls setting screen, I don’t think anyone would know what it means.

Related to that, I’ve been trying to set a new input for an existing InputMap. Input doesn’t have any get key press that I could use to read whatever the user presses, nor does InputEvent. I see InputMap has settings for removing and adding actions, but I’m not sure how to get a user action to set in InputMap.action_add_event("button", new_event)

:bust_in_silhouette: Reply From: colonelkurtz

Figured it out with some help:

event.as_text() shows the text for an event. For mouse button input, this shows quite a bit of info, and I found that event.button_index gives better results, though requires another step to convert numbers (number index of mouse buttons) to names like LMB, RMB, etc.

To get key presses and replace the current input map with the new key:

if event is InputEventKey and event.pressed:
 var old_button= InputMap.get_action_list("InputMap_button")
 InputMap.action_add_event("InputMap_button", event)