How can I use the back and forward mouse buttons in a Swing application?
Asked Answered
R

4

8

The question is pretty simple. I couldn't find many links regarding this issue, and the ones I found didn't seemed to avoid the real question. My application must handle the mouse pressed/released events for the back and forward mouse buttons. How can I handle this?

EDIT: This is using JDK 1.6.

Reprehensible answered 4/10, 2011 at 8:2 Comment(2)
What are back and forward buttons? My mouse has only 1 button ;-)Spread
@michael667: should have bought a PC ;-)Reprehensible
O
8

Check if additional mouse buttons are detected by calling:

MouseInfo.getNumberOfButtons();

Check if MouseEvents are fired when you click those additional buttons. If so, what does MouseInfo.getButton() return?

According to the javadocs for MouseInfo.getButton():

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

* 0 (NOBUTTON)
* 1 (BUTTON1)
* 2 (BUTTON2)
* 3 (BUTTON3)
* 4
* 5
Oxytocic answered 4/10, 2011 at 8:16 Comment(4)
+1: I did not know what happens with 5 buttons mouse. But, @dogbane, how can we distinguish between "back" and "forward" buttons? Can we be sure that button 4 is back and 5 is forward?Later
This only works with Java 7 and higher. Java 6 and below never returns 4 or 5 from MouseInfo.getButton() for any mouse.Manger
In JDK 1.6, the MouseEvent constructor throws an exception if the button is greater than 3. The Toolkit.areExtraMouseButtonsEnabled method was added in 1.7.Reprehensible
I have a problem with MouseInfo.getNumberOfButtons(); it returns 5, but my mouse only has three buttons (including the wheel button)Forbes
S
3

Have a look at MouseEvent.getButton() and Toolkit.areExtraMouseButtonsEnabled().

Spread answered 4/10, 2011 at 8:20 Comment(1)
Unfortunately I'm restricted to JDK 1.6, and that Toolkit method was added in 1.7.Reprehensible
D
3

Credit belongs to the original responders, just adding a ready-to-use code sample for global back-/forward-button detection in case it helps anyone 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);
}
Doscher answered 23/10, 2017 at 15:27 Comment(1)
Button 4 and 5 are mousewheel left and right for me. Is there any way of detecting which button id belongs to which button?Reach
D
0

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

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

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

If back/forward are now supported then I would guess they have added:

isBackMouseButton(MouseEvent)
isForwardMouseButton(MouseEvent) 
Doall answered 4/10, 2011 at 15:11 Comment(4)
-1 for not even looking to see if those methods exist (they don't so this doesn't even answer the question if I could use JDK 7). Back/forward buttons are just side buttons on a mouse. Not sure how you haven't heard of it - they've been on every mouse I've purchased for at least the last decade.Reprehensible
@280Z28 What part of "I don't use JDK7" do you not understand? How could I read the API if I don't have access to it? I was trying to be helpful to point people to the place where I though it would be logical to add this support in JDK7. You should never hard code magic numbers, which is why you should be using SwingUtilities to know which button was pressed. My comment about back/forward buttons was regarding support in Swing JDK6. Also,the question I answered was actually posed by AlexR. Not sure the best way to reply since you can't post code in a comment. Knew it wouldn't help you in JDK6.Doall
@camickr: I don't downvote to spite people. Any objective analysis of your post reveals that it does not (at all) assist someone looking for an answer to my question. Any frustration in my tone is due to how annoying this problem is, not to your post specifically. However, I really need an actual answer to my question so I'm being more of a stickler for +/- than I might otherwise be.Reprehensible
@280Z28 sorry for trying to help someone other than you. I highlighted the question I was trying to answer. As it turned out my thought process was wrong. I still suggest the JDK7 implementation is incomplete. If Back/Forward buttons are supported you should not have to use "magic numbers" to determine which button was used. Anybody reading my answer objectively would have known it was a guess. I was very upfront about it being a guess. Most peope would have made a comment that the SwingUtilities API still does not support those values.Doall

© 2022 - 2024 — McMap. All rights reserved.