用于设置/退出全屏的相同KeyCode - JavaFx

时间:2018-01-30 19:32:53

标签: java javafx fullscreen keyevent keycode

我将此用于setFullScreen()

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent e) {
            if(e.getCode() == KeyCode.F11) {
                stage.setFullScreen(true);
            }
        }
    });

这一行退出它:

stage.setFullScreenExitKeyCombination(new KeyCodeCombination(KeyCode.F11));

正如您所看到的,我想对两者使用相同的KeyCode(F11)。但它没有正确退出fullScreen!我猜,它是在它退出之后设置的全屏。所以它不会关闭fullScreenMode。

2 个答案:

答案 0 :(得分:1)

这是您问题的最简单答案。

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent e) {
    if(e.getCode() == KeyCode.F11) {
        stage.setFullScreen(!stage.isFullScreen());
    }
}
});

答案 1 :(得分:0)

只需设置一个全屏为真/假的标志即可。

boolean fullScreen = false;            // This is global variable.
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent e) {
        if(e.getCode() == KeyCode.F11) {
            if(fullScreen)
               stage.setFullScreen(true);
             else
               stage.setFullScreen(false);
            // Toggling fullscreen variable after toggling full screen
            fullScreen = !fullScreen;
        }
    }
});