1

Using the System.currentTimeMillis( ) method how can I detect if at least 200ms have passed in between mouse clicks? I have the following method that passes in a boolean value that tells you if the mouse button is currently pressed down or not. This method is called every 20ms.

public void update(boolean mouseDown)

I thought about using a series of if statements but I just can't get it to work.

if(mouseDown)
        {
             click = true;
             waitTime = System.currentTimeMillis();
        }

        if(click && (!mouseDown) && (waitTime-System.currentTimMillis()) <= 200)
        {
          //You got a Double Click
        }
        else if(click && (!mouseDown))
        {
          //You got a Single Click
        }

The problem is since the method is called every 20ms it always registers it as a double click. It always enters the first if statement and resets the value so I never end up with a time that is greater then 200ms. Any idea how I can fix this?

  • 3
    Possible duplicate of [Distinguish between a single click and a double click in Java](http://stackoverflow.com/questions/4577424/distinguish-between-a-single-click-and-a-double-click-in-java) ...always search this site before posting a question. Especially with things that are very likely to have been asked before ... – GhostCat Oct 20 '15 at 21:34
  • Why are you using 2 different ids? You already have a related question in this posting: http://stackoverflow.com/questions/33188203/detect-a-mouse-click-using-a-boolean-value/33188592#33188592, which you accepted with your other id, even though the answer is wrong. A mousePressed event is not a mouseClick! I even gave you code for determining a single/double click. Well actually I had a simpler example to post but you didn't respond to the first answer so I though you were not interested. – camickr Oct 20 '15 at 21:41
  • Can't do that, I have to use the method provided. – Gabriela Marinho Oct 20 '15 at 21:45
  • My answer suggested you need to add the Timer logic to your update(...) method. I listed the 3 conditions you need to handle. I tested the suggestion and it seems to work fine (if I understand your requirements). – camickr Oct 20 '15 at 21:59

0 Answers0