如何在EventHandler中触发KeyEvent? (JavaFX的)

时间:2016-06-09 16:54:02

标签: javafx

假设我有EventHandler<KeyEvent> keyEvent = event -> {...};

我将如何调用此事件? fireEvent()需要Event,我试图加注Event fireEvent((Event) keyEvent);,但不行。

1 个答案:

答案 0 :(得分:1)

如果您的处理程序没有使用该事件,那么您可以执行

keyEvent.handle(null);

调用它。

如果确实如此,我会将其考虑到另一种方法,将您需要的信息作为参数。例如。如果你只需要KeyCode,你可以这样做:

private void processKeyPress(KeyCode code) {
    // ...
}

然后您的事件处理程序将是:

// surely keyEventHandler (or similar) is a better name than keyEvent:
EventHandler<KeyEvent> keyEventHandler = e -> processKeyPress(e.getCode());

现在,您可以在需要时调用processKeyPress方法,例如

processKeyPress(KeyCode.ENTER);

这是一个使用键入事件中的字符的简单示例。请注意按钮上的动作事件处理程序和场景上的键入处理程序如何调用processKey方法:

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class NumberPad extends Application {

    private final ObjectProperty<Number> value = new SimpleObjectProperty<>(new Integer(0));

    private final StringProperty trailingZeros = new SimpleStringProperty("");

    private boolean decimalPressed = false ;
    private int numDecimalPlaces = 0 ;

    private final Label display = new Label();


    private void processKey(char c) {
        if (c >= '0' && c <= '9') {
            int val = c - '0' ;
            if (decimalPressed) {
                if (val == 0 && numDecimalPlaces > 0) {
                    trailingZeros.set(trailingZeros.get()+"0");
                } else {
                    trailingZeros.set("");
                }
                numDecimalPlaces++ ;
                value.set(new Double(value.get().doubleValue() + val * Math.pow(10, -numDecimalPlaces)));
            } else {
                value.set(new Integer(value.get().intValue() * 10 + val));
            }
        } else if (c == '.') {
            decimalPressed = true ;
        } else if (c == 'C') {
            value.set(new Integer(0));
            decimalPressed = false ;
            numDecimalPlaces = 0 ;
            trailingZeros.set("");
        }
    }

    private Button createButton(char c) {
        Button button = new Button(Character.toString(c));

        button.setOnAction(e -> processKey(c));

        button.setFont(Font.font(24));
        button.setMinSize(25, 25);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

        GridPane.setFillHeight(button, true);
        GridPane.setFillWidth(button, true);
        return button ;
    }

    @Override
    public void start(Stage primaryStage) {

        display.setFont(Font.font(24));
        display.textProperty().bind(value.asString().concat(trailingZeros));

        GridPane root = new GridPane();
        root.setHgap(5);
        root.setVgap(5);
        root.setPadding(new Insets(10));

        root.add(display, 0, 0, 3, 1);

        Scene scene = new Scene(root);
        scene.setOnKeyTyped(e -> processKey(e.getCharacter().charAt(0)));

        for (int value = 0 ; value <= 9; value++) {
            char c = (char)('0' + value) ;

            Button button = createButton(c);

            if (value == 0) {
                root.add(button, 0, 3);
            } else {
                int row = 3 - (value - 1) / 3 ;
                int col = (value - 1) % 3 ;
                root.add(button,  col, row);
            }

        }

        root.add(createButton('.'), 1, 4);
        root.add(createButton('C'),  2,  4);

        primaryStage.setScene(scene);
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}