user8491933
user8491933

Reputation:

Same KeyCode for setting/exiting fullscreen - JavaFx

I use this to setFullScreen():

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

And this line to exit it:

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

And as you can see, I want to use the same KeyCode (F11) for both. But it doesn't exit the fullScreen correctly! I guess, it's setting the fulscreen just after it exited it. So it doesn't close the fullScreenMode.

Upvotes: 1

Views: 373

Answers (2)

nitishk72
nitishk72

Reputation: 1746

This is the easiest answer to your question.

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

Upvotes: 2

nitishk72
nitishk72

Reputation: 1746

Just set a flag that fullscreen is true/false and that it.

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

Upvotes: 0

Related Questions