+4 votes

Hey,

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).

http://docs.godotengine.org/en/stable/classes/class_inputmap.html#class-inputmap-get-action-list

by (7,946 points)

Thanks,

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
http://docs.godotengine.org/en/stable/classes/[email protected]%20scope.html

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:
        print(OS.get_scancode_string(action.scancode))

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:
        print(OS.get_scancode_string(action.scancode))

but it didn't work in 3.1
so i change

if action.type == InputEvent.KEY:

to

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 = ""

    InputMap.get_actions()
    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)):
            continue

        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.