2

I'm trying to trigger backspace key on virtual keyboard for touch based system. this is my code which I tried so far.

Button source = new Button("Backspace");
TextField target = new TextField();
KeyEvent ke = new KeyEvent(source, target, KeyEvent.KEY_TYPED, "", "", KeyCode.BACK_SPACE, false, false, false, false);
target.fireEvent(ke);

this code is return nothing...

Hiran
  • 287
  • 4
  • 19
  • Why not just do [`target.deletePreviousChar()`](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextInputControl.html#deletePreviousChar--)? – James_D Oct 21 '15 at 16:33
  • Thanks @James_D but I need to know that is there any convenient way to use Backspace in KeyEvent?? – Hiran Oct 21 '15 at 16:48
  • Why do you need to do that? – James_D Oct 21 '15 at 16:49
  • because there are lot of meta keys and how do I invoke them?? – Hiran Oct 21 '15 at 16:59
  • Not sure I understand that comment. See answer anyway. Also, [here](https://github.com/james-d/Virtual-keyboard) is a virtual keyboard I wrote ages ago; it's probably not quite right but still seems to work quite nicely. – James_D Oct 21 '15 at 17:28

1 Answers1

3

You can directly make the TextField behave as though the backspace key has been pressed by calling target.deletePreviousChar(), which is probably a far better approach.

To mimic the actual pressing of the Backspace key, you need the following changes:

  • The text field reacts to KEY_PRESSED events, not KEY_TYPED events. It's probably best to generate the whole sequence of events that happen when you type a key: KEY_PRESSED, KEY_TYPED, and KEY_RELEASED.
  • The text field must have focus. You can prevent the button from getting the focus with source.setFocusTraversable(false).
  • The KeyCode for KEY_TYPED events should be UNDEFINED (see docs).

Here's a SSCCE:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TriggerBackspaceOnTextField extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField textField = new TextField();
        Button backspace = new Button("Backspace");
        backspace.setFocusTraversable(false);
        backspace.setOnAction(e -> {
            KeyEvent press = new KeyEvent(backspace, textField, KeyEvent.KEY_PRESSED, "", "", KeyCode.BACK_SPACE, false, false, false, false);
            textField.fireEvent(press);
            KeyEvent typed = new KeyEvent(backspace, textField, KeyEvent.KEY_TYPED, "", "", KeyCode.UNDEFINED, false, false, false, false);
            textField.fireEvent(typed);
            KeyEvent release = new KeyEvent(backspace, textField, KeyEvent.KEY_RELEASED, "", "", KeyCode.BACK_SPACE, false, false, false, false);
            textField.fireEvent(release);
        });

        VBox root = new VBox(10, textField, backspace);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 350, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322