How to handle typing special characters?

Godot Version

v4.2.2.stable.official [15073afe3]


I am making a typing game but I don’t know how to handle special characters like the underscore, where you have to press shift to access it.

if event is InputEventKey and not event.is_pressed():
		var typed_event = event as InputEventKey
		var has_shift = typed_event.shift_pressed
		var key_typed
		match has_shift:
				key_typed = char(typed_event.keycode)
				key_typed = char(typed_event.keycode).to_lower()

if i press shift±, it does not give me the underscore. it prints shift (�) and -. i tried using as_text_keycode() (just printed out Shift±) and key_label, and i’m not too sure how to use get_keycode_with_modifiers().

do i have to do these manually?

Your current code does not work with different keyboard layouts. This should fix the underscore problem and this also works with different layouts:

func _input(event):
	if event is InputEventKey && event.is_pressed() && !event.is_echo():
			var typed_event = event as InputEventKey
			var key_str: String = PackedByteArray([typed_event.unicode]).get_string_from_utf8()