Search code examples
javajavafxkeyeventkeycode

Cannot find symbol event.getKeyCode()


I am trying to convert KeyCode to string and after reading from other help, .getKeyCode() is the answer to converting KeyCode to String. However after adding it, an error says that it "cannot find symbol" on .getKeyCode(). There is another KeyEvent import but if use that import rather than the current one I use, the error disappears but the program cannot run.

This is my Controller class:

package keyboardrecorder;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;

public class Controller {

    @FXML
    private TextArea consoleKeyTyped;
    private TextArea consoleKeyPressed;
    private TextArea consoleKeyReleased;

    public void outputKeyTyped(KeyEvent event) {
        consoleKeyTyped.setText(consoleKeyTyped.getText() + event.getCharacter());
    }

    public void outputKeyPressed(KeyEvent event) {
         consoleKeyPressed.setText(consoleKeyPressed.getText() + event.getKeyCode());
    }

    public void outputKeyReleased(KeyEvent event) {

    }

}

This is my FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="keyboardrecorder.Controller">
   <children>
      <TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Key Typed">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyTyped" editable="false" onKeyTyped="#outputKeyTyped" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Key Pressed">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyPressed" editable="false" onKeyPressed="#outputKeyPressed" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab text="Key Released">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyReleased" editable="false" onKeyReleased="#outputKeyReleased" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

Solution

  • javafx.scene.input.KeyEvent is not java.awt.event.KeyEvent .

    You want getCode() instead of getKeyCode() .