Share this page 

Detect the mouse button used when clickingTag(s): AWT


[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) {
    switch(e.getModifiers()) {
      case InputEvent.BUTTON1_MASK: {
        System.out.println("That's the LEFT button");     
        break;
        }
      case InputEvent.BUTTON2_MASK: {
        System.out.println("That's the MIDDLE button");     
        break;
        }
      case InputEvent.BUTTON3_MASK: {
        System.out.println("That's the RIGHT button");     
        break;
        }
      }
    }
}