Java - get the number of mouse buttons

Java mouse buttons FAQ: How do I determine the number of mouse buttons in a Java application?

I'm working on a Java Swing application, and I'd like to be able to determine the number of mouse buttons on the current system (Mac, Linux, Windows).

Java number of mouse buttons - Solution

To get the number of mouse buttons in a Java/Swing app, just use the getNumberOfButtons() method in the Java MouseInfo class to determine the number of mouse buttons. Here's a quick Java source code example to demonstrate this:

public class MouseButtonTest
{
  public static void main(String args[])
  {
    System.out.println(java.awt.MouseInfo.getNumberOfButtons());
  }
}

Java number of mouse buttons - Sample results

Note that if for some reason a system doesn't have a mouse, the getNumberOfButtons method will return a value of -1.

I just ran this on my MacBook Pro, and it returned 1, which is essentially correct. You have to press the [Ctrl] key while using the keypad to simulate the right-mouse click.

The funnier thing was running this on my iMac, which has an Apple Wireless Mighty Mouse. It returns a value of 4. Wow, that really is a mighty mouse, lol.