当在 TextField 中按下回车键时,JavaFX 程序切换全屏

JavaFX program toggles full-screen when enter key is pressed inside of a TextField

我让我的程序在同时按下键盘上的两个键 alt 和 enter 时进入全屏模式。这基本上按预期工作。

问题是只要按下回车键,我的程序就会切换全屏模式。按下alt键也没关系

如何才能让程序在只按下回车键时不切换全屏模式。

我正在使用 OpenJFX 11。

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;


public class Main extends Application {
   final KeyCombination FullScreenKeyCombo = 
         new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_ANY);

    @Override
    public void start(Stage stage) {
            GridPane grid = new GridPane();
            Scene scene = new Scene(grid, 1600, 900);
            stage.setScene(scene);
            stage.show();

         // create TextField and add to GridPane
         TextField textField = new TextField();
         grid.add(textField, 0, 0);

         // toggle full-screen when alt + enter is pressed
         scene.addEventHandler(KeyEvent.KEY_PRESSED, event -> {

            if(FullScreenKeyCombo.match(event)) {

               stage.setFullScreen(!stage.isFullScreen());

            }
         });

    }

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

这一行:

new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_ANY);

ALT_ANY 表示“我不在乎是否按下 Alt 键。”

改用ALT_DOWN

new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_DOWN);