user23033721
user23033721

Reputation: 37

How do i get the lowercase version of a key of InputEventKey in Godot?

I am making a typing game in Godot v4. I used this code from youtube, but the print statement prints out the uppercase version of the key despite not having caps lock on or pressing shift. is this normal godot behavior? How do i get the lowercase version when i'm not pressing shift/dont have caps lock on?

func _unhandled_input(event:InputEvent) -> void:
    if event is InputEventKey and not event.is_pressed():
        var typed_event = event as InputEventKey
        var key_typed = PackedByteArray([typed_event.get_keycode_with_modifiers()]).get_string_from_utf8()
        print(key_typed)

I tried using keycode instead of get_keycode_with_modifiers() and i also tried key_label but they do the same thing. I am fairly new to godot and I've been trying to look for answers but have not found any.

Upvotes: 1

Views: 357

Answers (1)

Michael
Michael

Reputation: 1

You need to check if shift is being pressed with the button. You can actually check this modifier on your event.

func _unhandled_input(event:InputEvent) -> void:
    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 = PackedByteArray([typed_event.get_keycode_with_modifiers()]).get_string_from_utf8()
        
        match has_shift:
            true:
                print(key_typed)
            false:
                print(key_typed.to_lower())

With this new check, we can use a match to see if our new variable is true or false. If it's true, we don't need to change anything since it is already uppercase by default. If you want to be safe you can do key_typed.to_upper(). If has_shift comes back as false, we call to_lower() to convert our string to lowercase.

Upvotes: 0

Related Questions