I am doing a project in JavaFX, using Java 8 and Gluon scenebuilder. I want to detect when backspace
is pressed inside a TextField
. This is the code I am using:
public void keyPressed(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.BACK_SPACE) {
System.out.println("Backspace pressed");
}
This code is inside the controller file, called FXMLDocumentController
, which controls the GUI xml file FXMLDocument
. You can see from the image below the function is called whenever a key is typed inside TextField
. This works with all the letters/numbers, but not with backspace
.
Theoretically it should work, but it doesn't.
How can I manage typing of the backspace
button?
Edit:
Notice that putting this exact function on the root of the elements, on the "Window itself" (which is the AnchorPane
) works. The problem is with reading the pressing of the backspace
inside the TextField
. You can see in the image below where I've put the function:
You should use your keyPressed
method in either on key pressed
or on key released
.
In the Docs, it states that:
No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).
Backspace is consider an Action Key
.