I've seen this done in Godot 3, and I can't for the life of me figure out how it's done in Godot 4 since they changed the code.
G3:
for action in InputMap.get_action_list("interact"):
if action.type == InputEvent.KEY:
print(OS.get_scancode_string(action.scancode))

I got close in G4:
var key_event: Array[InputEvent] = InputMap.action_get_events("interact")
print(key_event[0])
It gives me this:
InputEventKey: keycode=69 (E), mods=none, physical=true, pressed=false, echo=false
OS.something cannot connect to the above line.

I'm trying to get the E key to present to the user as a prompt as to what to press for the action "interact" that I set up in Project Settings/Input Map.
Any help would be great.

Do something like :

			var input_events = InputMap.action_get_events(ui_action)
			for input_event in input_events:
				if input_event is InputEventKey:
					if scan_code == input_event.scancode:

Because there can be more than one key for an event

2 months later

I'm trying the same, but for some reason InputEventKey.keycode always return 0

Code:

var action_events = InputMap.action_get_events("Interact")[0]
var KeyCode = action_events.keycode
var KeyString = OS.get_keycode_string(KeyCode)
print(str(action_events))
print("Keycode: " + str(KeyCode))
print("String: " + KeyString)

Output:

InputEventKey: keycode=70 (F), mods=none, physical=true, pressed=false, echo=false
Keycode: 0
String:

Edit: for those who had the same problem, you had to use a equivalent, if key or physical key
in my case i'ts physical, so I had to use InputEventKey.physical_keycode, InputEventKey.keycode in this case will return 0

  • xyz replied to this.

    Felipai If your action is defined as a physical keycode you should look at physical_keycode property instead of keycode.

    a year later

    Yes, @xyz is correct:

    var key_name =""
    //assigning your input action from Project Settings Input Map
    var prompt_action = "yourInputAction"
    if InputMap.has_action(prompt_action):
    // will depend on how many keys assigned to action
    var key_action = InputMap.action_get_events(prompt_action)[0]
    var key_string = OS.get_keycode_string(key_action.physical_keycode)
    key_name = str(key_string