How do I detect holding down the mouse?

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

Using If Input.is_action_pressed() does not work for me for some reason. It results in just one input as opposed to a continuous one.

I want to know how I would be able to detect holding input.

:bust_in_silhouette: Reply From: Ertain

As mentioned in this answer, it may be better to use the function Input.is_mouse_buttoned_pressed( mouse_number ) for detecting mouse clicks and when they’re held down. In the _process() function:

func _process( some_change ):
    if Input.is_mouse_button_pressed( 1 ): # Left click
          # Perform some stuff.

In the _input() function:

var mouse_left_down: bool = false
func _input( event ):
    if event is InputEventMouseButton:
          if event.button_index == 1 and event.is_pressed():
                mouse_left_down = true
         elif event.button_index == 1 and not event.is_pressed():
                mouse_left_down = false
func _process( some_change ):
    if mouse_left_down:
                # Perform some stuff.

If you didn’t know, when using Input.is_action_pressed( some_action ), it’s usually used in the _process() function because it detects the input each frame.

Hope that helps.