Get keys from InputMap (GDScript)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sparks
:warning: Old Version Published before Godot 3 was released.

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")
:bust_in_silhouette: Reply From: eons

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

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 :wink:

sparks | 2017-04-12 18:34

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

eons | 2017-04-13 01:18

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.

Hammer Bro. | 2017-07-12 23:03

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

ruruarchy | 2019-06-23 04:51

:bust_in_silhouette: Reply From: DavidPeterWorks

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