This question is related to this one .
The application has three main screens, the way to handle the events on the ESCAPE key works only on one of the three (the main one, loaded at the start), for the others it is not detected key, although events are always captured in the same way.
Class with the code that controls the keys pressed:
/**
* @file: KeyInteractivityHandler.java
* @package: tasktimer.controllers
*
* @proyect: TaskTimer
* @author: Moisés Alcocer, 2018
* @link: http://www.ironwoods.es/herramientas/TaskTimer
* @license: http://www.apache.org/licenses/ - Apache License 2.0
*
*/
package tasktimer.controllers;
import javafx.scene.input.KeyEvent;
import javafx.scene.Scene;
import static javafx.scene.input.KeyCode.ALT;
import static javafx.scene.input.KeyCode.CONTROL;
import static javafx.scene.input.KeyCode.ENTER;
import static javafx.scene.input.KeyCode.ESCAPE;
import static javafx.scene.input.KeyCode.TAB;
import tasktimer.App;
/**
* Class to handle key events
*
*/
public class KeyInteractivityHandler {
/******************************************************************/
/*** Properties declaration ***************************************/
/******************************************************************/
/*** Methods declaration ******************************************/
/*** Public Methods ***************/
/**
* Controls the key events over "Settings Window"
*
* @param scn
* @param app
*/
public static void controlSettingsWindow(Scene scn, App app) {
scn.setOnKeyReleased((KeyEvent keyEvent) -> {
System.out.println(" -> " + keyEvent.getCode().toString( )); // HACK: trace
switch (keyEvent.getCode()) {
case ESCAPE:
closeApp(app);
break;
case ALT:
app.openStadisticsWindow();
break;
case TAB:
app.openTimerWindow();
break;
}
});
}
/**
* Controls the key events over "Stadistics Window"
*
* @param scn
* @param app
*/
public static void controlStadisticsWindow(Scene scn, App app) {
scn.setOnKeyReleased((KeyEvent keyEvent) -> {
System.out.println(" -> " + keyEvent.getCode().toString( )); // HACK: trace
switch (keyEvent.getCode()) {
case ESCAPE:
closeApp(app);
break;
case CONTROL:
app.openSettingsWindow();
break;
case TAB:
app.openTimerWindow();
break;
}
});
}
/**
* Controls the key events over "Timer Window"
*
* @param scn
* @param app
*/
public static void controlTimerWindow(Scene scn, App app) {
scn.setOnKeyReleased((KeyEvent keyEvent) -> {
System.out.println(" -> " + keyEvent.getCode().toString( )); // HACK: trace
switch (keyEvent.getCode()) {
case ESCAPE:
closeApp(app);
break;
case ENTER:
swapTimerStatus(app);
break;
case CONTROL:
app.openSettingsWindow();
break;
case ALT:
app.openStadisticsWindow();
break;
}
});
}
/*** Private Methods **************/
/**
* Closes the App
*
* @param app
*/
private static void closeApp(App app) {
app.saveCounters(true);
System.exit(0);
}
/**
* Starts or stops the timer
*
* @param app
*/
private static void swapTimerStatus(App app) {
if (app.timerController.isTimerActive( )) {
app.timerController.stopTimer();
} else {
app.timerController.startTimer();
}
}
} // class
Note: I did not know if the loading order of the screens was interfering with the detection, so I tried opening one of the other screens at the start. The result was the same, the ESCAPE key is only detected in the main screen, whether it is opened at the beginning or after one of the secondary ones.