你所面臨的問題可以通過添加日誌監聽來解釋:
textField.addEventFilter(Event.ANY, e -> System.out.println(e));
所以,當你輸入一個密鑰,你得到這個:
KeyEvent [source = [email protected][styleClass=text-input text-field], target = [email protected][styleClass=text-input text-field], eventType = KEY_PRESSED, consumed = false, character = , text = 1, code = NUMPAD1]
KeyEvent [source = [email protected][styleClass=text-input text-field], target = [email protected][styleClass=text-input text-field], eventType = KEY_TYPED, consumed = false, character = 1, text = , code = UNDEFINED]
KeyEvent [source = [email protected][styleClass=text-input text-field], target = [email protected][styleClass=text-input text-field], eventType = KEY_RELEASED, consumed = false, character = , text = 1, code = NUMPAD1]
換句話說,的keyTyped事件可以」 t區分鍵盤按下(代碼= UNDEFINED),所以你不能抓住它。
它與異常一起工作的原因與您只是在不檢查任何內容的情況下使用該事件相同。您可以使用它並防止打字。但是,不要用鍵盤檢查。
但是再一次,你不應該阻止它。它應該取決於用戶想要使用他的鍵盤或任何輸入機制。
但是,如果你必須,你必須。鑑於此信息解決此問題可能是:
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
HBox root = new HBox();
EventHandler<KeyEvent> handler = new EventHandler<KeyEvent>() {
private boolean willConsume = false;
@Override
public void handle(KeyEvent event) {
if (willConsume) {
event.consume();
}
if (event.getCode().isKeypadKey()) {
if (event.getEventType() == KeyEvent.KEY_PRESSED) {
willConsume = true;
} else if (event.getEventType() == KeyEvent.KEY_RELEASED) {
willConsume = false;
}
}
}
};
TextField textField = new TextField();
textField.addEventFilter(KeyEvent.ANY, handler);
// logging
textField.addEventFilter(KeyEvent.ANY, e -> System.out.println(e));
root.getChildren().addAll(textField);
Scene scene = new Scene(root, 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
這是簡單的版本。其實你必須使用一個擴展的機制來處理多個按鍵。