Corona SDK Pro Tip of the Day #11

Easily take screenshots of your app or game with a press of a single button!

Corona now supports input from a keyboard. Let's bind "S" key to capture screenshots with

Put this code in your main.lua file.

Runtime:addEventListener('key', function (event)  
  if event.keyName == 's' and event.phase == 'down' then
    local scene = storyboard.getScene(storyboard.getCurrentSceneName())
    if scene and scene.view then, display.pixelWidth .. 'x' .. display.pixelHeight .. '_' .. math.floor(system.getTimer()) .. '.png')
      return true

This adds a listener for 'key' events. Inside the listener we check if the pressed key is 's'. 'down' phase represents the moment the key was pressed, not released.

Then we need to get the current scene being displayed and save it's content with I use here storyboard, but it's easy to convert to composer.

local scene = composer.getScene(composer.getSceneName('current'))  

Inside we give the screenshot a distinct filename with a format WIDTHxHEIGHT_time. Screenshots are saved to Documents directory inside project's sandbox.

And because some devices can have keyboards attached, use it only in the Simulator.

It is also available as a GitHub Gist.

Indie Game Developer