how to properly detect which mouse buttons are down in JavaFX

12,925

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");
        }
    }
});
Share:
12,925

Related videos on Youtube

Chechulin
Author by

Chechulin

Skype: chechulin_anton

Updated on June 04, 2022

Comments

  • Chechulin
    Chechulin about 2 years

    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?