Return to the Start Menu when pressing ESC with JavaFx

3

How can I get my application back to the start menu after pressing ESC . Currently I can use KeyEvent only when some component is associated, such as TextField , Button , etc. I just want to press Esc without any component being selected and the application returns to the main menu. Example of how I do (Associating a component):

txtPesquisar.setOnKeyPressed(k -> {
            final KeyCombination ENTER = new KeyCodeCombination(KeyCode.ENTER);
            if (ENTER.match(k)) {
                atualizar();
            }
        });
    
asked by anonymous 24.06.2015 / 16:05

1 answer

1

Just add a handler to your scene.

scene.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
    if (t.getCode() == KeyCode.ESCAPE) {
        //codigo para ir ao menu inicial
    }
});
    
23.08.2015 / 08:11