Sometimes on Mac, BorderPane will not consume the event, thus it will be passed to an inclosed control, a button for example. To reproduce:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button b = new Button("Button");
b.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event) {
if (event.isShortcutDown()) {
System.out.println(event.getCode() + ", " + event.getCharacter() + " pressed in Button, shortcut: " + event.isShortcutDown());
event.consume();
}
}
});
final BorderPane p = new BorderPane();
p.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event)
{
if (event.isShortcutDown()) {
System.out.println(event.getCode() + ", " + event.getCharacter() + " pressed in BorderPane, shortcut: " + event.isShortcutDown());
event.consume();
}
}
});
p.setCenter(b);
Scene s = new Scene(new VBox());
((VBox)s.getRoot()).getChildren().add(p);
stage.setScene(s);
stage.show();
}
}
The output will be from the both controllers.
P.S. I have tried to change the 'setOnKeyPressed' to 'setOnKeyTyped' and/or change 'keyEvent.KEY_PRESSED' to 'keyEvent.KEY_TYPED', but has the same results.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button b = new Button("Button");
b.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event) {
if (event.isShortcutDown()) {
System.out.println(event.getCode() + ", " + event.getCharacter() + " pressed in Button, shortcut: " + event.isShortcutDown());
event.consume();
}
}
});
final BorderPane p = new BorderPane();
p.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event)
{
if (event.isShortcutDown()) {
System.out.println(event.getCode() + ", " + event.getCharacter() + " pressed in BorderPane, shortcut: " + event.isShortcutDown());
event.consume();
}
}
});
p.setCenter(b);
Scene s = new Scene(new VBox());
((VBox)s.getRoot()).getChildren().add(p);
stage.setScene(s);
stage.show();
}
}
The output will be from the both controllers.
P.S. I have tried to change the 'setOnKeyPressed' to 'setOnKeyTyped' and/or change 'keyEvent.KEY_PRESSED' to 'keyEvent.KEY_TYPED', but has the same results.