import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ListView; import javafx.scene.control.TextBox; import javafx.stage.Stage; import javafx.scene.input.KeyEvent; public class KeyEvents extends Application { final ListView console = new ListView(); //create a observableArrayList of logged events that will be listed in console final ObservableList consoleObservableList = FXCollections.observableArrayList(); { //set up the console console.setItems(consoleObservableList); console.setLayoutY(60); console.setPrefSize(400, 195); } final TextBox textBar = new TextBox("Write here"); @Override public void start(Stage stage) throws Exception { Group root = new Group(); Scene scene = new Scene(root, 500, 400); stage.setScene(scene); textBar.setPrefSize(200, 35); textBar.setLayoutX(100); textBar.setStyle("-fx-font-size: 14;"); textBar.setOnKeyPressed(new EventHandler() { public void handle(KeyEvent ke) { showOnConsole("Key Pressed: " + ke.getText()); } }); textBar.setOnKeyReleased(new EventHandler() { public void handle(KeyEvent ke) { showOnConsole("Key Released: " + ke.getText()); } }); textBar.setOnKeyTyped(new EventHandler() { public void handle(KeyEvent ke) { String text = "Key Typed: " + ke.getCharacter(); showOnConsole(text); } }); root.getChildren().addAll(textBar, console); stage.setVisible(true); } public static void main(String[] args) { Application.launch(args); } private void showOnConsole(String text) { //if there is 8 items in list, delete first log message, shift other logs and add a new one to end position if (consoleObservableList.size() == 8) { consoleObservableList.remove(0); // consoleObservableList.clear(); } consoleObservableList.add(text); } }