Detect a double click vs simple clickTag(s): AWT


[JDK1.02]
import java.applet.*;
import java.awt.*;

public class d extends Applet {

  int dClkRes = 300;    // double-click speed in ms
  long timeMouseDown=0; // last mouse down time
  int lastX=0,lastY=0;  //  last x and y

  public boolean mouseDown(Event event, int x, int y){
    /*
    ** check for double click
    */
    long currentTime = event.when;
    if ((lastX==x) && (lastY==y) && 
       ((event.when-timeMouseDown) < dClkRes)) {
        System.out.println("double click " + currentTime);    
        return false;
    }
    else {
        //single click action could be added here
        System.out.println("simple click " + currentTime);
        timeMouseDown = event.when;
        lastX=x;
        lastY=y;
    }
    return true;
  }
}
[JDK11]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class d extends Applet
 implements MouseListener {
 
  public void init() {
    this.addMouseListener(this);
    }
 
  public void paint(Graphics g) {
     g.drawString("Click here", 10,10);
    }

  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {
    System.out.println
      ( "Click at (" + e.getX() + ":" + e.getY() + ")" );
    if (e.getClickCount() == 2)  
      System.out.println( "  and it's a double click!");
    else
      System.out.println( "  and it's a simple click!");
    }
}

blog comments powered by Disqus