KeyListener JavaFX


I was looking on the internet but I didn't find good information for it. I'm trying to detect key presses every time the app is running. I'm using JavaFX and running it with FXML. I tryed a lot of thing but none work. Please help me.

4/18/2014 10:14:04 PM

Accepted Answer

You should check out the Ensemble sample. Here's the key listener code.

 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

 * A sample that demonstrates various key events and their usage. Type in the
 * text box to view the triggered events: key pressed, key typed and key 
 * released. Pressing the Shift, Ctrl, and Alt keys also trigger events.
 * @see javafx.scene.input.KeyCode
 * @see javafx.scene.input.KeyEvent
 * @see javafx.event.EventHandler
public class KeyEventsSample extends Application {

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        //create a console for logging key events
        final ListView<String> console = new ListView<String>(FXCollections.<String>observableArrayList());
        // listen on the console items and remove old ones when we get over 20 items in the list
        console.getItems().addListener(new ListChangeListener<String>() {
            @Override public void onChanged(Change<? extends String> change) {
                while ( {
                    if (change.getList().size() > 20) change.getList().remove(0);
        // create text box for typing in
        final TextField textBox = new TextField();
        textBox.setPromptText("Write here");
        textBox.setStyle("-fx-font-size: 34;");
        //add a key listeners
        textBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                console.getItems().add("Key Pressed: " + ke.getText());
        textBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                console.getItems().add("Key Released: " + ke.getText());
        textBox.setOnKeyTyped(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent ke) {
                String text = "Key Typed: " + ke.getCharacter();
                if (ke.isAltDown()) {
                    text += " , alt down";
                if (ke.isControlDown()) {
                    text += " , ctrl down";
                if (ke.isMetaDown()) {
                    text += " , meta down";
                if (ke.isShiftDown()) {
                    text += " , shift down";
        VBox vb = new VBox(10);
        vb.getChildren().addAll(textBox, console);

    @Override public void start(Stage primaryStage) throws Exception {
    public static void main(String[] args) { launch(args); }
10/29/2016 5:57:07 PM

This worked for me:

At the FXML add the onKeyPressed attribute in the element. Here is an example, note that the onKeyPressed attribute is in the AnchorPane element.

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" onKeyPressed="#handleKeyPressed"  xmlns:fx="" fx:controller="com.jtetris.jtetris.FXMLController">
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />

Next, add the method (handleKeyPressed) inside the controller

public class FXMLController implements Initializable {

  private Label label;

  private void handleButtonAction(ActionEvent event) {
      System.out.println("You clicked me!");
      label.setText("Hello World!");

  private void handleKeyPressed(KeyEvent ke){
    System.out.println("Key Pressed: " + ke.getCode());

  public void initialize(URL url, ResourceBundle rb) {
    // TODO

Finally, load the fxml in the start method.

public class MainApp extends Application {

 public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));

    Scene scene = new Scene(root);

    stage.setTitle("JavaFX and Maven");
 public static void main(String[] args) {


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow