Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to properly detect which mouse buttons are down in JavaFX

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?

like image 715
Chechulin Avatar asked Oct 10 '12 10:10

Chechulin


1 Answers

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");
        }
    }
});
like image 59
Sergey Grinev Avatar answered Sep 22 '22 18:09

Sergey Grinev