Invalid get index 'x'(on base:'InputEventMouseButton') and problem with set_input_as_handled()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fati98

So i was watching a tutorial about some functions about keyboard and set_process_unhandled_input.But at the code i wrote there are two problems:
1-When i click right mouse button i get a error “invalid get index ‘x’ (on base:‘InputEventMouseButton’).”
2-This code supposed to warn me when i press anythnig besides right and left keys.But it still warns me when i press right and left keys.

Here is the code:

extends Sprite

func _ready():


func _unhandled_input(event):

print(str("Yasak tuş",event))

func _input(event):

if(event is InputEventKey && event.is_pressed()):
	var yer = self.get("position")
	if(event.scancode == KEY_RIGHT):
		yer.x = yer.x + 10
	if(event.scancode == KEY_LEFT):
		yer.x = yer.x - 10

if(event is InputEventMouseButton && event.is_pressed()):

	if(event.button_index == BUTTON_LEFT):
:bust_in_silhouette: Reply From: timothybrentwood
if(event.button_index == BUTTON_LEFT):
    self.set("position",Vector2(event.position.x, event.position.y))


if(event.button_index == BUTTON_LEFT):
    self.set("position", event.position)

position is a property of the InputEventMouseButton. x and y are properties of the position property - which itself is a Vector2

“This code supposed to warn me when i press anything besides right and left keys. But it still warns me when i press right and left keys.

What do you mean by this? What is it doing to warn you?

:bust_in_silhouette: Reply From: PunchablePlushie

For the first part, the error is essentially saying that the InputEventMouseButton class doesn’t have any property called x.
If you want to get the mouse position with the event, you should use event.position. position is a Vector2 and a property of the InputEventMouse class, which is the parent of InputEventMouseButton.

For the second part, remember that pressing and releasing a key or mouse button creates two separate events: One is for pressing the key, and the other is for releasing it. So even tho the press event of your right and left keys get consumed and handled in the _input, the release event will remain unhandled, and it’ll get passed to _unhandled_input.
So, what you’re getting is the warning related to the unhandled release event of right or left keys.
To fix it, in the _unhandled_input(), simply check if event is pressed. So:

func _unhandled_input(event: InputEvent) -> void:
   if event.is_pressed():
      print("Yasak tuş", event)

That’ll make sure that the warning is printed only when a key is being pressed, not when it’s being released.