Topic was automatically imported from the old Question2Answer platform.
Asked By
unknown llama
Old Version
Published before Godot 3 was released.
Hello Everyone,
I have a very simple question this time!
How do I get the char value of a KEY that has been pressed in the _input(event) func and add it to a string?
I see that event.unicode seems to hold the integer value of the key but I cannot find a way to convert that integer into a char so it can be added to a string as a character not a number.
For Example:
If the user presses the ‘a’ key, event.unicode is 97, but I want the “a” to be added to my string, not “97”. In C we could just use char(97), but I cannot find a way to do this in GDScript.
I’m not sure about unicode, but you can use scancode and then convert it to the string with function OS::get scancode string. Dont forget to control the event type.
func _input(event):
if (event.type == InputEvent.KEY):
print(OS.get_scancode_string(event.scancode))
Thank you for your time and your answer.
Unfortunately scancodes are only uppercase and get_scancode_string returns full words for some of the codes, like “Shift” and “Space”
unknown llama | 2016-03-09 00:00
Unfortunately, that’s true. But you can check the length of string and filter out words like Shift, Space, etc. You can even check the scancode for CapsLock and Shift and use String::to_lower. Here is an example (just for Shift):