0 votes

why this gives me an error?

func _input(event):
    var keys = event as InputEventKey
    if keys.scancode == KEY_J:
        print(keys)
in Engine by (192 points)

1 Answer

0 votes

You're getting an error because you're currently processing all input, including mouse events (InputEventMouse), which doesn't have the scancode property.
You just need to use an if-statement to make sure it's a key press/release.

func _input(event):
    if !event.is_echo(): # ignore when key held down
        if event is InputEventKey:
            if event.is_pressed():
                var key_press: InputEventKey = event
                match key_press.scancode:
                    KEY_J:
                        print("Pressed J Key")
            else: # released
                var key_release: InputEventKey = event
                match key_release.scancode:
                    KEY_J:
                        print("Released J Key")
by (234 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 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 webmaster@godotengine.org with your username.