getting an action's event name

Godot Version

Godot 4.6 stable

Question

I’ve been trying to make one of those modular interaction systems, so that it shows a prompt of what key to press (when looking at an interactable). I’ve tried every combination of Key-taking methods, and all i managed was one function using physical keycodes, hence the first function below:

func get_action_key_names(action_name: String) -> String:
	if InputMap.has_action(action_name):
		for action in InputMap.action_get_events(action_name):
			if action is InputEventKey:
				var key_name = action.as_text_physical_keycode()
				return key_name
	return "Unknown"

And it works fine enough don’t get me wrong, but I don’t like having it be correct mainly on US Qwerty keyboards (i have an Azerty French one myself). Anyone on here with any ideas, or am i just missing too much?

(Lord i’ve spent a whole afternoon trying to get this to work and i’m just stuck)

I highly recommend this plugin: GitHub - godotneers/G.U.I.D.E: Godot Unified Input Detection Engine - A Godot extension to handle all input in a streamlined way · GitHub

Here are video guides for it: https://www.youtube.com/@godotneers/videos

The plugin @MrWetsnow linked to is pretty cool, and I like the idea of unified input. But I don’t see that it solves your problem.

I’m going to offer up my own Controller Plugin. Because it does exactly what you are trying to do. (Among other things.)

From the README:

Let’s say you have an “interact” action defined. When you call Controller.get_action_icon("interact") you will receive a Texture2D you can assign to a TextureRect and the UI will tell the player what the interact button is based on whether they last moved the mouse or gamepad joystick, and will return it based on the device they are currently using. (They’ll get an Xbox button for example if they are using an XBox controller, or a keyboard key if they’re using that.)

You can connect to the Controller.input_method_changed signal to update your UI immediately if the player switches devices.

You can test it out by playing my game Eternal Echoes and swapping between input devices.


(These screenshots were taken seconds apart.)

One caveat is I haven’t tested gamepads other than XBox since they updated the input libraries in 4.6. So if you have issues, please log an issue and I’ll fix it.

If you don’t like the images I’ve used, you can update them with your own very easily. If you want to add French keyboard keys, let me know. I have some ideas of how to tie that into localization. I just don’t have a French keyboard to test it.

as_text_physical_keycode links to a sample getting keycode for (hopefully) other keyboard layouts

func _input(event):
	if event is InputEventKey:
		var keycode = DisplayServer.keyboard_get_keycode_from_physical(event.physical_keycode)
		var label = DisplayServer.keyboard_get_label_from_physical(event.physical_keycode)
		print(OS.get_keycode_string(keycode))
		print(OS.get_keycode_string(label))

Godot Docs

3 Likes