package sample;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
primaryStage.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
TextArea t = (TextArea) root.lookup("#text");
t.appendText("Key Pressed: " + ke.getCode() + "\n");
if (ke.getCode() == KeyCode.F1) {
System.out.println("Key Pressed: " + ke.getCode());
ke.consume(); // <-- stops passing the event to next node
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}