Search code examples
javalibgdx

How to enable fullscreen when pressing a key in LibGDX


Hello kinda new to LibGDX and currently having issues with the fullscreen mode on pc, what i'm trying to do is set my game to fullscreen whenever someone presses a key and this doesn't do anything whenver i type something in the main method in desktopLauncher.java. And importing the LwjglApplicationConfiguration class in the core map doesn't work because it isn't available there for some reason.


Solution

  • I tested the answer provided by PandaBR on this question and it works. https://gamedev.stackexchange.com/a/119867/81956

    Put this in the render method of your main LibGDX / Game class (not DesktopLauncher). Change "Input.Keys.TAB" to be the key you want to toggle your program in and out of full screen mode.

    if (Gdx.input.isKeyPressed(Input.Keys.TAB)){
        Boolean fullScreen = Gdx.graphics.isFullscreen();
            Graphics.DisplayMode currentMode = Gdx.graphics.getDisplayMode();
            if (fullScreen == true)
                Gdx.graphics.setWindowedMode(currentMode.width, currentMode.height);
            else
                Gdx.graphics.setFullscreenMode(currentMode);
    }