5

I've set a listener to my Pane so that it will detect mouse left and right buttons being down. But when I hold left mouse button, then press right one, previous action seem to lose it's effect!

My code:

root.setOnMouseDragged(new EventHandler<MouseEvent>(){
 @Override
 public void handle(MouseEvent t) {
 if(t.getButton() == MouseButton.PRIMARY) f1();
 if(t.getButton() == MouseButton.SECONDARY) f2();
 }
});

while holding LMB I have f1() running, but when I push RMB it seems like new event totally overwrites previous one: only f2() runs.

How can I separate this two events?

Georg Plaz
6,0185 gold badges43 silver badges66 bronze badges
asked Oct 10, 2012 at 10:08

1 Answer 1

10

getButton() can return only one value at a time. And it's latest pressed button. If you need to detect multiple mouse down being pressed you need to use corresponding functions:

root.setOnMouseDragged(new EventHandler<MouseEvent>() {
 @Override
 public void handle(MouseEvent t) {
 if (t.isPrimaryButtonDown()) {
 System.out.println("rockets armed");
 }
 if (t.isSecondaryButtonDown()) {
 System.out.println("autoaim engaged");
 }
 }
});
Georg Plaz
6,0185 gold badges43 silver badges66 bronze badges
answered Oct 10, 2012 at 10:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.