Display the character of an input on screen

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

I’m very new to godot and i want to have a message that displays on screen when the character can interact with an object (eg “press ‘action key’ to interact”). I’ve managed to figure out most of the things that i need to do, the only issue is i can’t figure out how to make the given input key a string and make it return the correct value. say if i had a key called “action key” and i set it to “F” I’d want the message to say “press F to interact”. any help would be very much appreciated.

:bust_in_silhouette: Reply From: Cearaj

You can do the trick with the use of scancodes and a few methods. Below is an explanation with some steps, as well as the full example code.


1. First, you can get the list (Array) of events attached to a certain action using the __`InputMap.get_action_list(action)`*__ method, `action` being a String. This Array has the same order as set in the Project Settings, so you may search for the key you want in whichever way you like. **For the sake of simplicity, however, I will assume that you'll always want the first event in the list.** You can simply index 0 like this: **`var event = InputMap.get_action_list(action)[0]`**
  1. Make sure the event you got is an InputEventKey, otherwise the rest of the code will throw an error. (see InputEventKey)
    This only covers keyboard input*, the approach for mouse and controller input is briefly explained in the bottom of the answer.

  2. Now, you can get the key scancode through either event.scancode or event.physical_scancode properties.
    For regular key events, scancode will have the value you want while physical_scancode will be 0. The opposite happens for physical key events.
    In order to support both, you can just use an if statement to check whether or not physical_scancode is different from 0:

  • If it is, you need to convert it using
    and save that.
  • If it’s not, you just save scancode instead.
  1. Finally, you’re going to get the name of the key using this method:
    (Considering scancode the one saved in the previous step).

Full Example Code

This is how I put it together in a function:
func get_key_name(action: String):
    # Gets the first input event attached to an action.
    var event = InputMap.get_action_list(action)[0]
    # Returns if the event is not a key.
    if not event is InputEventKey:
    # Gets the constant scancode and the physical scancode of the key
    var scancode: int = event.scancode
    var physical_scancode: int = event.physical_scancode
    # Checks if the key is physical, if so it converts the scancode
    if physical_scancode:
        scancode = OS.keyboard_get_scancode_from_physical(physical_scancode)
    # Returns the name of the key
    return OS.get_scancode_string(scancode)


- Beware that, if you happen to type an action that does not exist, the third line will give you an **index error**. You can check if it exists beforehand using **`InputMap.has_action(action)`** in case you want to avoid that and perhaps add a custom warning or error.
  • As mentioned in step 2, this solution only supports keyboard. If you also want to show the names of mouse buttons or gamepad buttons, the approach is pretty different and you’ll have to add more to your code.
    I will go over this briefly:
  • For mouse events, you’re gonna have to check for the event being an InputEventMouseButton like we do in step 2.
    Then, you should be able to figure out what are the buttons using event.button_index, which is an integer corresponding to a ButtonList constant.
    (see InputEventMouseButton)

  • For gamepad events, it’s basically the same approach as mouse, using the event type InputEventJoypadButton and the enumerator JoypadList instead.
    (see InputEventJoypadButton)


- https://forum.godotengine.org/135316/how-do-i-get-a-keys-name - https://github.com/godotengine/godot/pull/56015 - https://docs.godotengine.org/en/3.5/

Don’t hesitate to ask me for more details in case I missed anything!

:bust_in_silhouette: Reply From: Coenster

You can make it a string by using “as_text()”

You can print the key by doing:

print(InputMap.get_action_list("action key")[0].as_text())

In your case it would be something like this assuming you are using a label :

var the_key = InputMap.get_action_list("your_action_name")[0].as_text()
$label.set_text(str("Press " + the_key + " to interact"))
1 Like