How to use the back and front mouse buttons in a Swing app?

The question is pretty simple. I could not find many references to this problem, and those that I found did not escape the real question. My application should handle clicked / released mouse events for the back and forward buttons. How can I handle this?

EDIT:. Used by JDK 1.6 .

+7
source share
4 answers

Check if additional mouse buttons are detected by calling:

MouseInfo.getNumberOfButtons();

Check if MouseEvents are running when you click these extra buttons. If so, what MouseInfo.getButton() return?

According to javadocs for MouseInfo.getButton () :

If a mouse with five buttons is installed, this method can return the following values:

 * 0 (NOBUTTON) * 1 (BUTTON1) * 2 (BUTTON2) * 3 (BUTTON3) * 4 * 5 
+8
source
+3
source

How can we distinguish between the back and forward buttons? Can we be sure that button 4 is back and 5 is back?

I do not use JDK7 and have never heard of the back / forward buttons. However, I know that the SwingUtilities class has methods:

 isRightMouseButton(MouseEvent) isLeftMouseButton(MouseEvent) isMiddleMouseButton(MouseEvent) 

If back / forward support is now supported, I would suggest that they add:

 isBackMouseButton(MouseEvent) isForwardMouseButton(MouseEvent) 
+1
source

The credit belongs to the original respondents, simply by adding a ready-to-use sample code for global search in back-to-forward mode if it helps someone else (JDK 1.8)

 if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() && MouseInfo.getNumberOfButtons() > 3) { Toolkit.getDefaultToolkit().addAWTEventListener(event -> { if (event instanceof MouseEvent) { MouseEvent mouseEvent = (MouseEvent) event; if (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED && mouseEvent.getButton() > 3) { if (mouseEvent.getButton() == 4) { // back } else if (mouseEvent.getButton() == 5) { // forward } } } }, AWTEvent.MOUSE_EVENT_MASK); } 
+1
source

All Articles