Physical Back Button?

How can i make my game come up with a menu, instead of shutting the app down, when i press the physical back button on android, or the physical button on the iphone.

I want players to be able to use the physical buttons to navigate trough my game menus, and put the game on pause for example.

And for example a confirmation pop up window, when pressing the back button, when you are in the menu and want to exit the game with the back button would be nice too, more user friendly.

Anyway you get the point, I want to use the physical button(s) from the devices.
But I cant really seem to find much about the physical buttons, is this possible in corona?
And do I have to make a different code on android then on iPhone/iPad, for this buttons?

And if it is, is it possible in the trial version? We do not yet have the funds to purchase corona (just started our company) so im still building it in the trial version until most of it is done before purchasing. [import]uid: 118839 topic_id: 25964 reply_id: 325964[/import]

*Bump* Anyone? [import]uid: 118839 topic_id: 25964 reply_id: 105666[/import]

The documentation is kind of buried, but what you’re looking for is the “key” event listener and events:

http://developer.anscamobile.com/reference/index/events/key

The short version is this:

[lua]-- Key listener
local function onKeyEvent( event )
return true
end

– Add the key callback
Runtime:addEventListener( “key”, onKeyEvent );[/lua]

That will make all the supported keys do nothing. See the API doc linked for the various keys and events supported by the API. [import]uid: 44647 topic_id: 25964 reply_id: 105668[/import]

i have a problem with the code above. Im using the code with storyboard and put it in every storyboard that i make. when testing on an android device the app crashes. I could only use this code once? I want make the back button go to a previous storyboard. [import]uid: 135155 topic_id: 25964 reply_id: 105722[/import]

If you’re using it with storyboard, you’re going to want to put the addEventListener in the enterScene() function and remove it in the exitScene() function. Every storyboard scene will have its own function telling the buttons how to behave, but you only want one eventlistener at a time. [import]uid: 44647 topic_id: 25964 reply_id: 105740[/import]

Are your referring to the “home” button when talking about the “back” button on the device?

Thats a no go - you can’t attach any listener to the hardware buttons. If you do active this in some way, Apple will reject your app.

Joakim [import]uid: 81188 topic_id: 25964 reply_id: 105745[/import]

He’s talking about the Android hardware buttons like Back, Menu, and Search.

Joakim does make a good point – don’t activate the key listener in your iOS builds. Use some conditional logic before applying the runtime listener if you’re doing cross-platform development. [import]uid: 44647 topic_id: 25964 reply_id: 105750[/import]

Whats an Android :wink:

Sorry :slight_smile: [import]uid: 81188 topic_id: 25964 reply_id: 105752[/import]

thanks. will try and report back. i was talking about the android back button [import]uid: 135155 topic_id: 25964 reply_id: 105813[/import]

after adding Runtime:removeEventListener( “key”, onKeyEvent ) to every storyboard it works and stopped crashing [import]uid: 135155 topic_id: 25964 reply_id: 106034[/import]