extends Node
var test: Dictionary[InputEventKey, String]
func _ready() -> void:
var key := InputEventKey.new()
key.keycode = KEY_1
test[key] = "String 1"
print(test)
var key_2 := InputEventKey.new()
key_2.keycode = KEY_2
test[key_2] = "String 2"
print(test)
var key_3 := InputEventKey.new()
key_3.keycode = KEY_1
test[key_3] = "String 3"
print(test)
test[key] = "String 4"
print(test)
key.keycode = KEY_4
print(test)
Output:
{ InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 1” }
{ InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 1”, InputEventKey: keycode=50 (2), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 2” }
{ InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 1”, InputEventKey: keycode=50 (2), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 2”, InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 3” }
{ InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 4”, InputEventKey: keycode=50 (2), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 2”, InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 3” }
{ InputEventKey: keycode=52 (4), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 4”, InputEventKey: keycode=50 (2), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 2”, InputEventKey: keycode=49 (1), mods=none, physical=false, location=unspecified, pressed=false, echo=false: “String 3” }
As you can see from my little test, Objects as Dictionary keys are supported by Godot. They are compared by reference. So two Objects with the same values, but different memory locations will evaluate as different.
If you don’t like that comparison, you can do it on your own as @normalized points out. But this feels more now like a question of why GDScript doesn’t allow you to do something that other languages do. If you really think it should, open up a feature request.