You have to remove the semi-colon. From

if(event.getCode() == KeyCode.ENTER); {

To

if(event.getCode() == KeyCode.ENTER) {

Also, in Swing I know for sure you can add an ActionListener to a JTextBox directly, and it'll trigger the action event when enter is pressed while the JTextBox as focus. It'll prevent having your action being triggered in unrelated context. Hopefully something like that exist in JavaFX!

Answer from Alexandre on Stack Overflow
🌐
Oracle
docs.oracle.com › javafx › 2 › api › javafx › scene › input › KeyCode.html
KeyCode (JavaFX 2.2)
javafx.scene.input · java.lang.Object · java.lang.Enum<KeyCode> javafx.scene.input.KeyCode · All Implemented Interfaces: java.io.Serializable, java.lang.Comparable<KeyCode> public enum KeyCode extends java.lang.Enum<KeyCode> Set of key codes for KeyEvent objects.
🌐
Oracle
docs.oracle.com › javase › 8 › javafx › api › javafx › scene › input › KeyEvent.html
KeyEvent (JavaFX 8)
2 weeks ago - The key code associated with the key in this key pressed or key released event. For key typed events, code is always KeyCode.UNDEFINED.
🌐
Oracle
docs.oracle.com › javase › 8 › javafx › api › javafx › scene › input › class-use › KeyCode.html
Uses of Class javafx.scene.input.KeyCode (JavaFX 8)
July 15, 2025 - JavaFX 8 · Prev · Next · Frames · No Frames · All Classes · Skip navigation links · Overview · Package · Class · Use · Tree · Deprecated · Index · Help · JavaFX 8 · Prev · Next · Frames · No Frames · All Classes · Copyright (c) 2008, 2015, Oracle and/or its affiliates.
🌐
Java Tips
javatips.net › api › javafx.scene.input.keycode
Java Examples for javafx.scene.input.KeyCode - Javatips.net
private void onKeyPress(KeyEvent event) { if (event == null || event.getCode() == null) { return; } final KeyCode code = event.getCode(); if (code == KeyCode.UP) { caretPos = -1; moveCaret(textField.getLength()); } else if (code == KeyCode.DOWN) { if (!popup.isShowing()) { final Point2D popupPos = textField.localToScreen(0, textField.getHeight()); if (popupPos != null) { popup.sizeToScene(); popup.show(textField, popupPos.getX(), popupPos.getY()); } } caretPos = -1; moveCaret(textField.getLength()); } else if (code == KeyCode.BACK_SPACE) { caretPos = textField.getCaretPosition(); } else if (co
🌐
OpenJFX
openjfx.io › javadoc › 11 › javafx.graphics › javafx › scene › input › KeyCodeCombination.html
KeyCodeCombination (JavaFX 11)
JavaFX 2.0 · KeyCombination.Modifier, KeyCombination.ModifierValue · ALT_ANY, ALT_DOWN, CONTROL_ANY, CONTROL_DOWN, META_ANY, META_DOWN, NO_MATCH, SHIFT_ANY, SHIFT_DOWN, SHORTCUT_ANY, SHORTCUT_DOWN · getAlt, getControl, getMeta, getShift, getShortcut, keyCombination, toString, valueOf · clone, finalize, getClass, notify, notifyAll, wait, wait, wait · public KeyCodeCombination​(KeyCode code, KeyCombination.ModifierValue shift, KeyCombination.ModifierValue control, KeyCombination.ModifierValue alt, KeyCombination.ModifierValue meta, KeyCombination.ModifierValue shortcut) Constructs a KeyCodeCombination for the specified main key and with an explicit specification of all modifier keys.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › javafx › api › javafx › scene › input › KeyCode.html
KeyCode (JavaFX 8)
2 weeks ago - JavaFX 2.2 · public final String getName() Gets name of this key code. Returns: Name of this key code · public static KeyCode getKeyCode(String name) Parses textual representation of a key. Parameters: name - Textual representation of the key · Returns: KeyCode for the key with the given name, null if the string is unknown.
🌐
Program Creek
programcreek.com › java-api-examples
javafx.scene.input.KeyCode Java Examples
@Override @JmeThread protected void registerActionHandlers(@NotNull final ObjectDictionary<String, BooleanFloatConsumer> actionHandlers) { super.registerActionHandlers(actionHandlers); final T fileEditor = getFileEditor(); actionHandlers.put(KEY_S, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.S, isPressed, isControlDown(), isShiftDown(), isButtonMiddleDown())); actionHandlers.put(KEY_C, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.C, isPressed, isControlDown(), isShiftDown(), isButtonMiddleDown())); actionHandlers.put(KEY_P, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.P, isPressed, isControlDown(), isShiftDown(), isButtonMiddleDown())); actionHandlers.put(KEY_L, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.L, isPressed, isControlDown(), isShiftDown(), isButtonMiddleDown())); }
🌐
Tabnine
tabnine.com › home page › code › java › javafx.scene.input.keycode
javafx.scene.input.KeyCode java code examples | Tabnine
public class KeysMapper { private static HashMap<Character, Integer> charMap = new HashMap<Character, Integer>(); static { for (KeyCode keyCode : KeyCode.values()) { if (keyCode.impl_getCode() >= 65 && keyCode.impl_getCode() <= 90){ charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode()); } else{ charMap.put(keyCode.getName().toLowerCase().toCharArray()[0], keyCode.impl_getCode()); } } } public static Key charToKey(char c){ if(c>=65 && c<=90){ return new Key(charMap.get(c), true); } else { return new Key(charMap.get(c), false); } } public static List<Key> stringToKeys(String text){ List<Key> keys = new ArrayList<Key>(); for (char c : text.toCharArray()) { keys.add(charToKey(c)); } return keys; }
🌐
OpenJFX
openjfx.io › javadoc › 11 › javafx.graphics › javafx › scene › input › KeyCode.html
KeyCode (JavaFX 11)
Package javafx.scene.input · java.lang.Object · java.lang.Enum<KeyCode> javafx.scene.input.KeyCode · All Implemented Interfaces: Serializable, Comparable<KeyCode> public enum KeyCode extends Enum<KeyCode> Set of key codes for KeyEvent objects. Since: JavaFX 2.0 ·
🌐
YouTube
youtube.com › bro code
JavaFX KeyEvent ⌨️ - YouTube
JavaFX KeyEvent tutorial example explained#javafx #keyevent #keys
Published   March 15, 2021
Views   26K
🌐
GitHub
github.com › puce77 › openjfx-9-dev-rt › blob › master › modules › graphics › src › main › java › javafx › scene › input › KeyCode.java
openjfx-9-dev-rt/modules/graphics/src/main/java/javafx/scene/input/KeyCode.java at master · puce77/openjfx-9-dev-rt
* @since JavaFX 2.0 · */ public enum KeyCode { · /** * Constant for the {@code Enter} key. */ ENTER(0x0A, "Enter", KeyCodeClass.WHITESPACE), · /** * Constant for the {@code Backspace} key. */ BACK_SPACE(0x08, "Backspace"), ·
Author   puce77