+1 vote

I am trying to implement the dialog system and I want to choose my dialog options using keyboard's numericals keys (e.g 1,2,3...). I can create an InputEvent for every number or I can handle ten InputEventKeys manually but it looks strange because the logic for every event is the same.

What I want is to unite those events into one InputEvent group, parse the particular key that was pressed and use it as a variable for my function. Is it even possible in Godot? I cannot find any appropriate method in the official documentation https://docs.godotengine.org/en/stable/classes/class_inputevent.html#class-inputevent

   {
        if (@event.IsActionPressed("numerical")) {
            // I want to do smth like this
            InputEventKey key = @event.GetKey() 
            DoSmth(key)
    }

Edit:
My final solution below. Thanks to Gluon for the hint about scancodes. More information can be found here

// 'numeric' includes keys from '1' to '9'

if (@event.IsActionPressed("numeric")) {
    if (@event is InputEventKey eventKey) {
        DoSmth(eventKey.Scancode - 48);  // Scancode of '1' is 49
    }
}
Godot version godot v3.5.1.stable.mono.official [6fed1ffa3]
in Engine by (16 points)
edited by

1 Answer

0 votes
Best answer

The only way I know of personally is using scancodes so if you hav esomething like this

func _input(event):
    (print(event.scancode))

then this will print out the scancode of the button pressed. Scancodes can be seen here,

https://docs.godotengine.org/en/stable/classes/[email protected]?highlight=scancodes

but that isnt human readable. Someone else may have a better way but given you havent had an answer yet I thought I would at least give you this idea.

by (3,321 points)
selected by

That's perfect, thank you! It doesn't matter that scancodes unreadable, much more important is that they are ordered. I've updated my post with the final solution

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.