How do I detect touch input in a Web build?

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

Title pretty much sums it up. I’ve tried the following code in my Panel:

func _on_gui_input(event: InputEvent):
    if event is InputEventScreenTouch and event.is_pressed():
        # Run code...

Apparently this doesn’t work. Using InputEventMouseButton to detect clicks also doesn’t work on mobile. Is there any way to detect touch input with the gui_input event?

:bust_in_silhouette: Reply From: ababen

there are a few things you may want to check:

  1. does it detect your input when you click the panel with the mouse? if not, there might be a control node blocking the input event.
  2. If you go to project settings, and then to input_devices/pointing, you’ll see that there’s an option to emulate mouse clicks from touch, make sure the option is turned on.
  3. try doing this from a different browser and see if it’s any different

I’ve checked these things, but it turned out I might’ve been double clicking with the touch because I was detecting any mouse input.

if event is InputEventMouseButton and event.is_pressed()

Changing it to only check the left click worked:

if event is InputEventMouseButton and event.is_pressed() and event.button_index == 1:

Thanks anyways!

popcar2 | 2023-03-29 13:16