JavaFX8 KeyEvent - TextField上的触发器退格键

时间:2015-10-21 12:09:18

标签: javafx-8 keyevent backspace

我试图触发虚拟键盘上的退格键以用于基于触摸的系统。这是我到目前为止尝试的代码。

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);

此代码无效...

1 个答案:

答案 0 :(得分:2)

您可以通过调用TextField直接使target.deletePreviousChar()表现得像按下退格键一样,这可能是一种更好的方法。

要模仿实际按下 Backspace 键,您需要进行以下更改:

  • 文本字段会对KEY_PRESSED个事件做出反应,而非KEY_TYPED个事件。最好生成键入密钥时发生的整个事件序列:KEY_PRESSEDKEY_TYPEDKEY_RELEASED
  • 文本字段必须具有焦点。您可以使用source.setFocusTraversable(false)阻止该按钮获得焦点。
  • KeyCodeKEY_TYPED事件的UNDEFINED应为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); } } (请参阅文档)。

这是一个SSCCE:

private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
    AnimatorSet mAnimationSet = new AnimatorSet();
    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA,  1f, 0f);
    fadeOut.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            fadeOutTarget.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
    fadeOut.setInterpolator(new LinearInterpolator());

    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
    fadeIn.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            fadeInTarget.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {}

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
    fadeIn.setInterpolator(new LinearInterpolator());
    mAnimationSet.setDuration(duration);
    mAnimationSet.playTogether(fadeOut, fadeIn);
    mAnimationSet.start();
}