This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+6 votes

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.


in Engine by (80 points)

3 Answers

+2 votes
Best answer
var bla = RawArray([97])



More info

Edit: I do agree that a simpler function for single bytes would be great. You could add an issue on github about it.

by (372 points)
selected by

Perfect. Thank you for this!

I just discovered the get_string_from_utf8about 10 minutes ago, but was going about it the long way. Took me 4 lines to do what you have done in 1!

You have simplified it for me, thank you.

–1 vote

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):
by (836 points)

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" :(

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):

func _input(event):
if (event.type == InputEvent.KEY):
    if(OS.get_scancode_string(event.scancode).length() == 1):
+4 votes

Looks a new function was added specifically for this purpose later in 2016:

To convert event.unicode to a String, we can now do this: char(event.unicode)

by (144 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.