What is the difference between "if event is InputEventKey:" and not?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bgegg
func _input(event):
	if event.pressed and event.scancode == KEY_A:
		get_tree().quit()

func _unhandled_input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_A:
            get_tree().quit()

The result is the same
which one is correct?

However, the first code seems to cause an error when moving the mouse
I don’t know why

From my understanding if event is InputEventKey: narrows down what type of input the following if statements should expect. Without it the event input isn’t filtered from mouse or keyboard and possibly cause errors.
I’m not sure if this is right though which is why I made it a comment :confused:

Magso | 2019-11-06 11:33

Thank you
The answer to filtering was easy to understand

bgegg | 2019-11-07 01:43

:bust_in_silhouette: Reply From: Asheraryam

The code in _input crashes because you are assuming event has a “pressed” property, but not all events have it e.g. mouse movement is a different event type with different properties.

That’s why you have to check for which events you want to use e.g. InputEventKey, InputEventMouseMotion, InputEventMouseButton, etc… because the events are not all the same.

See the list here for the different input events.

Yes
I understood it was filtering
It looks like it can cause trouble if you do n’t filter it

bgegg | 2019-11-07 02:24

:bust_in_silhouette: Reply From: Carl Marqs

unhandled_input help:

Called when an InputEvent hasn't been consumed by _input() or any GUI. The input event propagates up through the node tree until a node consumes it
For gameplay input, this and _unhandled_key_input() are usually a better fit than _input() as they allow the GUI to intercept the events first

The best approach if you’re just getting keys pressed is using _unhandled_key_input(), as it only catches events after GUI elements :slight_smile:

Thank you
I understand it is a kind of filtering

bgegg | 2019-11-07 02:18