+1 vote

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")
asked Apr 12, 2017 in Engine by sparks (121 points)
edited Apr 12, 2017 by sparks

2 Answers

+4 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

answered Apr 12, 2017 by eons (7,764 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/class_@global%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    
answered Dec 9, 2018 by DavidPeterWorks (190 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 How to use this Q&A? before posting your first questions.