This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+4 votes


how can one retrieve the key(s) of an action set in the InputMap, in gdscript?
I search for something like that:

var array = InputMap.get_scancodes("ui_accept")
in Engine by (129 points)
edited by

2 Answers

+7 votes

InputMap use InputEvents, you can use InputMap.get_action_list("action") to get an array of InputEvents for that action.

Compare those events with the type and other details you are looking for (only KEY type have scancode).

by (7,954 points)


can I display those InputEvents like the according consts: e.g. KEY_F2
When I print this, it looks like the following:

[Device 0 ID 0 Event: Key Unicode: Scan: 16777245 Echo: False  ... ]

But I guess I have to write a custom script, where I add stuff to the InputMap and also adding stuff to some Dict, so I can combine it with names, and display it the way I want ..

Thanks anyway ;)

You can put all the numeric constant names on a file and check them there

I don't know if there is another way (but you may need that for things like translations anyway).

You can get the string value of the keyboard scancodes as follows:

for action in InputMap.get_action_list("ui_up"):
    if action.type == InputEvent.KEY:

Only InputEvent.KEY (InputEventKey) actions have scancodes.

whoa ! this is helpfull

for action in InputMap.get_action_list("ui_up"):
    if action.type == InputEvent.KEY:

but it didn't work in 3.1
so i change

if action.type == InputEvent.KEY:


if action is InputEventKey:

it works

thank you all,
thank you Hammer Bro

0 votes

I created a node that can convert keys to text.

it creates this output:
left = A
right = D
down = S
up = W
fire = Left mouse button

extends Node

var actionStartsWith ="player"

var messageFormat = "%s = %s"

func getKeyFromAction(action):
    var r = ""

    for a in InputMap.get_action_list(action):
        r += OS.get_scancode_string(a.scancode)

    return r

func getAllActionsAndKeys():
    var ret = ""

    for action in InputMap.get_actions():
        var actionString = str(action)
        if (!actionString.begins_with(actionStartsWith)):

        actionString = actionString.replace(actionStartsWith, "")

        for a in InputMap.get_action_list(action):
            if (a is InputEventKey):
                ret += messageFormat % [actionString, OS.get_scancode_string(a.scancode)]

            if (a is InputEventMouse):
                ret += messageFormat % [actionString, getMouseButtonText(a.get_button_index())]

            ret += "\r\n"

    return ret

func getMouseButtonText(index):
    if (index == 1):
        return "Left mouse button"

    if (index == 2):
        return "Middle mouse button"

    if (index == 3):
        return "Right mouse button"

    assert false    
by (327 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.