Event Handling - Keyboard Events
// JavaFX controls
// Keyboard Event handling
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.geometry.*;
public class EventHandling_KB extends Application
{
public static void main(String[] args)
{
// Start the JavaFX application by calling launch().
launch(args);
}
// Override the start() method.
public void start(Stage myStage)
{
StringBuffer str = new StringBuffer();
// Give the stage a title.
myStage.setTitle("JavaFx - Keyboard Event handling");
// Use a FlowPane for the root node.
FlowPane fp = new FlowPane(Orientation.VERTICAL,25,25);
// Center the controls in the scene.
fp.setAlignment(Pos.CENTER);
// Creating a heading and response text
Label heading = new Label("Keyboard Event Handling.");
Label respText = new Label("");
// Add to the scene graph.
fp.getChildren().addAll(heading, respText);
// Create a scene.
Scene myScene = new Scene(fp, 300, 100);
// Set the scene on the stage.
myStage.setScene(myScene);
// Event Handling
myScene.setOnKeyTyped(new EventHandler<KeyEvent>(){
public void handle(KeyEvent ke)
{
str.append(ke.getCharacter());
respText.setText(str.toString());
}
} ) ;
// Show the stage and its scene.
myStage.show();
}
}
// Sample Output
>javac --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls EventHandling_KB.java
>java --module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls EventHandling_KB










