Keycode event for backspace in JavaFx

I am doing a project in JavaFX using Java 8 and Gluon scenebuilder . I want to determine when backspacepressed inside TextField. This is the code I'm using:

public void keyPressed(KeyEvent kEvent) {
    if (kEvent.getCode() == KeyCode.BACK_SPACE) {
        System.out.println("Backspace pressed");
    }

This code is inside a controller file called FXMLDocumentControllerthat manages the xml xml file FXMLDocument. You can see in the image below that the function is called whenever a key is entered inside TextField. This works with all letters / numbers, but not with backspace.

Gluon Scene Builder Settings

Theoretically, it should work, but it is not.

How can I control button input backspace?

Edit:

Note that the use of this exact function in the root of the elements, in the "window itself" (which is AnchorPane). The problem is reading the click backspaceinside TextField. You can see in the image below where I put the function:

In the search box backspace works

+4
source share
2 answers

You must use your method keyPressedeither in on key pressedor on key released.

The Docs states that:

For keys that do not generate Unicode, key typed event symbols are not generated (for example, action keys, modifier keys, etc.).

Backspaceconsider Action Key.

+2
source

SceneBuilder , keyPressed(KeyEvent kEvent) On Key Typed .

KeyEvent.KEY_TYPED getCode() KeyCode.UNDEFINED.


: . - , , , - .

, "" AnchorPane, . AnchorPane , : On Key Pressed, On Key Released On Key Typed.

, ( AnchorPane). , ( - . Sedrick ), . - , .

, , AnchorPane, TextField, , AnchorPane, TextField.

+3

Source: https://habr.com/ru/post/1695548/


All Articles