Trying to rename a Label3d in game but the input is repeating twice, so the text adds to two letters for each inputs

Godot Version

4.6

Question

I had this problem in godot 4.5 and the code below somehow stop it. After migrating project to 4.6 I dont know if i changed the code or something changed internally.

I think you also need to check for event.is_pressed(). Otherwise it will include key releases, which could be the cause of your issue.

1 Like

Yes, the problem was I was getting two inputs for both pressing and releasing the input keys.

here the updated code.

func _unhandled_input(event: InputEvent) -> void:
	# Starts the renaming of the current Load File
	if event.is_action_pressed("f2"):
		var result := cast_ray()
		if not result: return
		var lab = result.collider.get_parent().get_child(-1)
		if lab is Label3D:
			get_input = true
			label = lab
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

	# Stores the input into a String
	if get_input and event is InputEventKey and event.is_pressed():
		var input := OS.get_keycode_string(event.keycode)
		if event.is_action_pressed("ui_text_backspace", true) and label.text:
			label.text = label.text.erase(label.text.length() - 1, 2)
		elif event.is_action_pressed("ui_cancel") or event.is_action_pressed("ui_accept"):
			get_input = false
			if label.text == "": label.text = "NEW SAVE" 
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		elif label.text.length() == 16: return
		elif input.length() == 1: label.text += input
		return