4

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.

Gluon scene builder settings

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:

On the window backspace's detecting works

F. Alessandro
  • 103
  • 3
  • 8
  • Does changing `KeyCode.BACK_SPACE` to `KeyCode.VK_BACK_SPACE` work? – mypetlion Mar 29 '18 at 19:54
  • I think VK_BACK_SPACE exists only on AWT, because the compiler says it can't find the symbol: `error: cannot find symbol. symbol: variable VK_BACK_SPACE, location: class KeyCode` – F. Alessandro Mar 29 '18 at 19:57
  • Can you log the value of the keycode and report back. Add `System.out.println(kEvent.getCode());` – SteelToe Mar 29 '18 at 20:35
  • @F.Alessandro The keyCode returns undefined when its a keyTyped event. I think a solution would be to use the getCharacter() method to get the unicode represecntation of backspace and then compare it to U+0008 which is the unicode for backspace – SteelToe Mar 29 '18 at 20:35
  • `System.out.println(kEvent.getCode());` returns exactly "BACK_SPACE". – F. Alessandro Mar 29 '18 at 20:38
  • if it returns that even when pressed inside the text field, so just change the if statement to `if(kEvent.getCode().toString.equals("BACK_SPACE"))` – SteelToe Mar 29 '18 at 20:41
  • Just checked again, both `System.out.println(kEvent.getCode());` and `System.out.println(KeyCode.BACK_SPACE);` return "BACK_SPACE" in the log. – F. Alessandro Mar 29 '18 at 20:43
  • Unfortunately the statement if `(kEvent.getCode().toString().equals("BACK_SPACE")) {...}` doesn't work inside the `TextField`, but works if the functions are on the root element of the GUI (as explained in the original post, under the "Edit" part). – F. Alessandro Mar 29 '18 at 20:48
  • @F.Alessandro did you try the method that i wrote above with using the getCharacter() method? – SteelToe Mar 29 '18 at 20:52
  • The statement `if (kEvent.getCode().toString().equals("\u0008")) {...}` doesn't work. I always get problems with UTF-8, is this right? – F. Alessandro Mar 30 '18 at 11:20

2 Answers2

3

In the screenshot of your SceneBuilder I can see you are referencing the keyPressed(KeyEvent kEvent) controller method with On Key Typed not On Key Pressed.

For events of KeyEvent.KEY_TYPED the value of getCode() is always KeyCode.UNDEFINED.


Edit: I came back to this answer and reread your question. I want to clarify something but what I said above is still correct.

In your edit you mention the "exact" same setup works with the AnchorPane but when looking at your screenshot there are some differences. For your AnchorPane you have the controller method referenced for all three types: On Key Pressed, On Key Released, and On Key Typed.

This means that the method should be called up to 3 times for each key stroke (when the events reach the AnchorPane). Once when pressed, once for typed (if the key is capable of sending typed events - see the answer by Sedrick for clarification), and then once for released. Because of this, the method will work for the pressed and released events, but it won't work for the typed events.

In other words, the reason your code works for the AnchorPane but not the TextField is because you configured it correctly for the AnchorPane but not the TextField.

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • 2
    Thanks for your answer! This helped me, but I accepted Sedrick's answer because he also explained why Backspace doesn't work on KeyTyped (because it is an Action Key and action keys don't generate any unicode characters) and he also referenced the documentation, which will eventually help anyone who gets here by any source. But don't worry you were very helpful too :D . – F. Alessandro Mar 30 '18 at 17:26
3

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.

SedJ601
  • 12,173
  • 3
  • 41
  • 59