List of pressed keys

can we have a list of PRESSED keys?

There’s no built-in function to do that but it’s easily done. Create an Autoload that keeps the pressed keys in an array like:

extends Node

var pressed_keys = []

func _input(event: InputEvent) -> void:
	if event is InputEventKey:
		if event.echo:
			# If the event is an echo event, skip it
			return
		if event.pressed:
			pressed_keys.push_back(event.keycode)
		else:
			pressed_keys.erase(event.keycode)

And you can use it anywhere in your project like:

extends Node


func _process(delta: float) -> void:
	var str = PressedKeys.pressed_keys.map(func(key:Key): return OS.get_keycode_string(key))
	$Label.text = "Pressed: %s" % ", ".join(str)

Result:

4 Likes

Thanks you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.