user23033721
user23033721

Reputation: 37

how do i handle shift+ characters in godot like the underscore?

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:
            true:
                key_typed = char(typed_event.keycode)
            false:
                key_typed = char(typed_event.keycode).to_lower()
        print(key_typed)

if i press shift+-, it does not give me the underscore. it prints shift (�) and -.

do i have to do these manually?

i tried using as_text_keycode() (just printed out Shift+-) and key_label did the same as when i used keycode.

Upvotes: 2

Views: 107

Answers (1)

MeSteve95
MeSteve95

Reputation: 131

It seems like what you're interested in here is getting the Unicode value of the current input, including special characters accessed by using Shift. This can be accomplished by using the event.unicode property.

An example of how this works is:

func _input(event):
    if event is InputEventKey:
        var unicode_character = event.unicode
    
        # Ignore non-character input
        if unicode_character == 0:
            return

        # Perform useful logic using unicode character
        print(char(unicode_character))

This will print each keyboard key that the user presses, including the special characters accessed by using Shift!

Upvotes: 0

Related Questions