By following this solution on Stack Overflow I have come up with the following Java class:
public final class Screenshot {
private static byte[] pixels;
private static Pixmap pixmap;
public static void take() {
pixels = ScreenUtils.getFrameBufferPixels(0, 0,
Gdx.graphics.getBackBufferWidth(),
Gdx.graphics.getBackBufferHeight(),
true);
pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(),
Gdx.graphics.getBackBufferHeight(),
Pixmap.Format.RGBA8888);
BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
PixmapIO.writePNG(Gdx.files.external("./screenshot_" + new Date().getTime() + ".png"), pixmap);
pixmap.dispose();
pixels = null;
}
}
Calling Screenshot().take()
will take the LibGDX render buffer and dump it in a PNG file. Exactly what I want.
My question is if there's a good way to 'capture' the print screen button. Is there a print screen keycode that I can use?