Search Unity

Differencing between sinlge click and double click?

Discussion in 'UI Toolkit' started by JoNax97, Apr 4, 2020.

  1. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Hi, I'm currently working on a desktop interface where I have icons that I can move around our double-click to fire an action. I'm having trouble to understand how I can distinguish between single clicks and double clicks, and specifically how not to drag if the click was double. I can't make it work using clickCount on the mouseDown event because the callback fires twice, once with clickCount == 1 and then with clickCount == 2, so I have to make a decision over something that has not happened yet. I hope that makes sense.

    Code (CSharp):
    1. private void OnMouseDown(MouseDownEvent e)
    2.          {
    3.              if (dragging)
    4.              {
    5.                  e.StopImmediatePropagation();
    6.                  return;
    7.              }
    8.  
    9.              if (e.button == 0)
    10.              {
    11.                  if (e.clickCount == 1)
    12.                  {
    13.                      dragging = true;
    14.                      this.CaptureMouse();
    15.                      // Start dragging
    16.                  }
    17.                  else if (e.clickCount == 2)
    18.                  {
    19.                     // Fire the other action
    20.                  }
    21.              }
    22.          }
    The code above would make sense if the event was fired only after the user stopped making successive clicks and reported the final amount of them, but that's not the case, so I'm a little lost. I'm sure I'm missing something but I can't wrap my head around it :(
     
    Last edited: Apr 4, 2020
  2. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
  3. EvgenyVasilyev

    EvgenyVasilyev

    Joined:
    Jul 5, 2019
    Posts:
    7
    Most systems that I know of handle this with some sort of double click delay. For example when a mouse click occurs a flag is set and then the time of the click is saved. So the next time a mouse click occurs we check if the flag was set and if it has we consider this a double click. If, however, a second click never comes, after a specified delay, we just simply register the click as a single click.

    I haven't tried this, but this maybe possible to do using the scheduler?
     
    JoNax97 likes this.
  4. Zeejfps

    Zeejfps

    Joined:
    May 20, 2017
    Posts:
    8
    I am the same guy as above. Try this. I haven't tested it but in theory should work.

    Code (CSharp):
    1.     bool _waitingOnSecondClick;
    2.     long _doubleClickDelay = 100; // In MS
    3.  
    4.     void OnMouseDown(MouseDownEvent e)
    5.     {
    6.         if (!_waitingOnSecondClick)
    7.         {
    8.             _waitingOnSecondClick = true;
    9.             this.schedule.Execute(()  =>
    10.             {
    11.                 // If this executes and we are still waiting for a second click, then it never happened.
    12.                 if (_waitingOnSecondClick)
    13.                 {
    14.                     // Handle single click
    15.                  
    16.                     // Reset the flag
    17.                     _waitingOnSecondClick = false;
    18.                 }
    19.             }).StartingIn(_doubleClickDelay);
    20.         }
    21.         // Can only be here if this is a double click
    22.         else
    23.         {
    24.             // Handle double click
    25.          
    26.             // Rest the flag
    27.             _waitingOnSecondClick = false;
    28.         }
    29.     }
     
    JoNax97 likes this.
  5. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Yeah that makes complete sense. I don't know why but I assumed that the event reporting a click count somehow meant that this was to be handled differently, and didn't think outside of that. Thanks a lot!
     
    EvgenyVasilyev likes this.