How to correctly determine which mouse buttons are not available in JavaFX

I installed the listener in my panel so that it detects that the left and right mouse buttons are down. But when I hold the left mouse button and then press the right one, the previous action seems to weaken the effect! What I have:

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

holding LMB, I have f1 (), but when I press RMB, it seems that the new event completely overwrites the previous one: only f2 () is fired. How can I separate these two events?

+4
source share
1 answer

getButton() can only return one value at a time. And this is the last button pressed. If you need to detect multiple mouse clicks, you need to use the appropriate 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"); } } }); 
+8
source

Source: https://habr.com/ru/post/1438861/


All Articles