Touch event repeating twice on android (SOLVED)

here is the GUI script:

settings = require "main.settings" 

function init(self)
        self.buttons =  { "btn_start","btn_stop" } 
        msg.post(".", "acquire_input_focus") 
end

function on_input(self, action_id, action)
   if action.pressed then
       ----------------------
        if action_id == hash("click") or action_id == hash("touch") then
            local x = action.x
            local y = action.y
            for i, n in ipairs(self.buttons) do
                if gui.pick_node(gui.get_node(n), x, y) then
                    ------------------
                    if n == self.buttons[1] then
                            settings.gameState = 100
                            print("start from main_menu")
                    elseif  n == self.buttons[2] then
                            print("stop from main_menu")
                    end
                    ------------------
                end
            end
        end    
    ----------------------    
    end 
end

on windows it works ok, but on android every touch on the button “btn_start” executes this block twice:

                    ------------------
                    if n == self.buttons[1] then
                            settings.gameState = 100
                            print("start from main_menu")
                    end
                    ------------------

What i’m doing wrong?

Do:

if action.pressed then
    print(action_id)

Could be that you receive both “click” and “touch” depending on how you mapped them to inputs.

mouse_trigger {
  input: MOUSE_BUTTON_1
  action: "click"
}
touch_trigger {
  input: TOUCH_MULTI
  action: "touch"
}


But i’ll try to separate “click” and “touch”

DEBUG:SCRIPT: hash: [touch]
DEBUG:SCRIPT: start from main_menu
DEBUG:SCRIPT: hash: [click]
DEBUG:SCRIPT: start from main_menu

It looks like “touch” and “click” are equal to each other on android and both working

The input manual mentions that you must create a binding for MOUSE_BUTTON_LEFT or MOUSE_BUTTON_1 to get single touch input on mobile devices. If you map both multi touch and single touch to the same action_id then you’ll probably get two “touch” events every time. Are you going to use multi touch? If so, assign it to another action_id than your single touch.

1 Like

Oh thank you. It looks like i’ve missed the point about single click events when study the manuals.
I’m using only single touches and swipes.

1 Like

Ok, good!

@sicher: We need to make this more prominent in the documentation. This isn’t the first time a user has overlooked this and mixed up single and multi-touch.

Yep, making a note about that.

Thank you!

I have code for MOUSE_BUTTON_1 event, and code for TOUCH_MULTI where the length of action.touch is 1. However the code for the MOUSE_BUTTON_1 event is firing (currently on android) even if I’m doing a multi-touch operation.

Do I have to add code to suppress this automatic left click event that’s firing regardless of whether multiple touches are active? FWIW, the single-touch code of the TOUCH_MULTI logic is also being run when I have one finger down (as another event fed to on_input), resulting in this operation being done twice anyway.

Could it be that the MOUSE_BUTTON_1 event firing is a bug, or is it some sort of android feature I need to work around that on my end? I’ll admit it’s convenient when coding something simple where a left-click or single tap is all I want. But considering it’s firing even when I do multiple touches, I think it might not be intentional.

In the meantime maybe the preferred solution for my code is to filter out the processing of mouse events if platform != desktop.

edit: Filtering of mouse events from sys.get_sys_info().system_name == “Android” or “iPhone OS” for now is fine, but probably not preferable in the future as more desktop OSs are accessed with multitouch screens

Hmm, I’m not sure if it’s intentional that both MOUSE_BUTTON_1 and TOUCH_MULTI fire during a multi-touch. @sven, @Mathias_Westerdahl?

Sorry for the resurrection of this thread, but I’m also curious about this, four years later!

With this game.input_binding:

mouse_trigger {
  input: MOUSE_BUTTON_1
  action: "touch"
}
touch_trigger {
  input: TOUCH_MULTI
  action: "multitouch"
}

One touch on an iPhone fires these on_input actions:

DEBUG:SCRIPT: Script: 0x0116edd250,
hash: [multitouch],
{ --[[0x116eed5a0]]
  x = 484.26669311523,
  touch = { --[[0x116eed870]]
    1 = { --[[0x116eed8d0]]
      x = 484,
      tap_count = 0,
      pressed = true,
      y = 262,
      screen_y = 999,
      screen_dy = 0,
      screen_dx = 0,
      dx = 0,
      dy = 0,
      id = 0,
      released = false,
      screen_x = 567
    }
  },
  pressed = true,
  y = 262.33169555664,
  screen_y = 999,
  screen_dy = 0,
  screen_dx = 0,
  screen_x = 567,
  value = 1,
  dx = 0,
  repeated = true,
  released = false,
  dy = 0
}
DEBUG:SCRIPT: Script: 0x0116edd250,
hash: [touch],
{ --[[0x116eedcd0]]
  x = 484.26669311523,
  pressed = true,
  y = 262.33169555664,
  screen_y = 999,
  screen_dy = -1437,
  screen_dx = 567,
  screen_x = 567,
  value = 1,
  dx = 483.84002685547,
  repeated = true,
  released = false,
  dy = -377.53692626953
}

Because two subsequent inputs are fired on the same frame, this makes it necessary to filter out one of them to avoid going back to the main menu twice and so on.

Why is mouse button 1 triggered, when it’s not actually in use?

What do you mean? The mouse button 1/left binding is generated on mobile for touch events. (https://defold.com/manuals/input/#trigger-types). We should not have made that decision way back when Defold was created, and if I was to redesign this part of the input code I would have only one touch binding and not even bother specifically with multi touch.

I didn’t know this was a deliberate design. I resurrected the thread because of this comment:

Thanks for clarifying.

Ah so it happens during a multi touch? I only saw one finger/touch registered in the touch table?

I thought if the table action.touch existed, that meant it was a multi touch?

I tested it with two touches, and mouse button 1 seems to fire (once per TOUCH_MULTI):

DEBUG:SCRIPT: Script: 0x01149dd250,
hash: [multitouch],
{ --[[0x114a027b0]]
  x = 651.52001953125,
  touch = { --[[0x114a02a20]]
    1 = { --[[0x114a02a80]]
      x = 651,
      tap_count = 0,
      pressed = true,
      y = 360,
      screen_y = 1371,
      screen_dy = 0,
      screen_dx = 0,
      dx = 0,
      dy = 0,
      id = 0,
      released = false,
      screen_x = 763
    },
    2 = { --[[0x114a02d90]]
      x = 298,
      tap_count = 0,
      pressed = true,
      y = 253,
      screen_y = 966,
      screen_dy = 0,
      screen_dx = 0,
      dx = 0,
      dy = 0,
      id = 1,
      released = false,
      screen_x = 349
    }
  },
  pressed = true,
  y = 360.0657043457,
  screen_y = 1371,
  screen_dy = 0,
  screen_dx = 0,
  screen_x = 763,
  value = 1,
  dx = 0,
  repeated = true,
  released = false,
  dy = 0
}
DEBUG:SCRIPT: Script: 0x01149dd250,
hash: [touch],
{ --[[0x114a03230]]
  x = 651.52001953125,
  pressed = true,
  y = 360.0657043457,
  screen_y = 1371,
  screen_dy = -1065,
  screen_dx = 763,
  screen_x = 763,
  value = 1,
  dx = 651.09332275391,
  repeated = true,
  released = false,
  dy = -279.80294799805
}

The way I would solve it is to check if the game is running on mobile in which case I would ignore “touch” events and only use “multitouch”.

1 Like