disable android back key

Hi all, I realize that there is a handler for key pressed in corona. Can anyone help me out with how I interject to stop the button from exiting the app? I don’t fully realize what I should be doing once the listener shows the back key is pressed. Thanks in advance. [import]uid: 42417 topic_id: 21992 reply_id: 321992[/import]

[lua]local hardwareKey = function(event)
if(event.phase == ‘down’) then
– call some function of yours here, passing event.keyName which is the name of the key that is down.
end
return true – this is very important.
end
Runtime:addEventListener( “key”, hardwareKey )[/lua]If you don’t return true in your handler function, the implied result is false (i.e. the key event was NOT handled.) If the key event is not handled by us it will be handled by the operation system, which, in the case of the back button, will exit your app. By returning true we can suppress this default behavior. [import]uid: 70391 topic_id: 21992 reply_id: 87420[/import]

Very nice. Thank you! I saw this in the docs but thought I had to actually do something beyond return true to muffle the button press. [import]uid: 42417 topic_id: 21992 reply_id: 87424[/import]

A couple of things here. You have to identify the back button, not just the button press. Secondly, any button press has two phases - up and down and the function will be executed twice for each press - must account for that.

As an example here is how I’m handling back button presses on Android:

local downPress = false  
function onBackButtonPressed(e)  
 if (e.phase == "down" and e.keyName == "back") then  
 downPress = true  
 else if (e.phase == "up" and e.keyName == "back" and downPress) then  
 -- do whatever (generally changing scene)  
 -- also don't forget to do this if changing scene: Runtime:removeEventListener( key,onBackButtonPressed)  
 end  
 end  
 return true  
end  
  
Runtime:addEventListener( "key", onBackButtonPressed )  

I hope this is helpful - it did bug me as well a while back.

Cheers,
Laurentiu [import]uid: 117906 topic_id: 21992 reply_id: 87449[/import]

@thinkpozzitive - Very useful sample code. Thanks for sharing.

BTW lets suppose I do not want to do nothing if the user press the hard Android back-button, SO should I just let the function “white/alone” to get this key “forgotten”?

PS: BTW is it allowed to be done? (ignore the click on any device`s button?)
Thanks,
Rodrigo. [import]uid: 89165 topic_id: 21992 reply_id: 87518[/import]

Yes it is allowed to do nothing…all your doing is handling the button press basically.

My code above does exactly nothing other than to handle the button press but without actually doing anything.

In order for it to do anything you’d have to do stuff were I’ve placed the comments. If you don’t do nothing, you will simply lock the “native” button functionality which in most cases leads to application exit, hence users pressing it won’t see anything happen, and pressing it won’t exit your application.

Best of luck,
Laurentiu [import]uid: 117906 topic_id: 21992 reply_id: 87656[/import]

@Laurentiu, Thank you! I really appreciate your time and explanation. :slight_smile:
Best Regards,
Rodrigo. [import]uid: 89165 topic_id: 21992 reply_id: 87658[/import]

@thinkpozzitive thanks for the awesome code! It helped a ton. I have a question though. When I use the code you provided it disables ALL android button(minus home button). How do I get it to only disable the back button? Thanks [import]uid: 46082 topic_id: 21992 reply_id: 113578[/import]

As far as I know the “home” key isn’t supported within Corona, or at least it wasn’t when I initially investigated. (more about this at the link provided below)

Take a look here at the event.keyName API: http://docs.coronalabs.com/api/event/key/keyName.html

Notice that in the code that I provided, the “back” key is caught by the runtime listener (which captures all supported key button presses), which then sends it to our handler.
In my example code, if you notice I make sure to identify the “back” button press, and do something with it, but I don’t do anything explicitly for the “menu” button.

To enable the “-” or menu/options key you simply need to check for that as well and make a conditional statement (pseudo-code e.g: if “back” then something elseif “menu” then something else).

Make sense? [import]uid: 117906 topic_id: 21992 reply_id: 113590[/import]

Hi sXc,

I guess the problem of yours just occurs because the “return true” statement tells Android that you handled all possible buttons.

If you only want to handle the “back” button you need to move the “return true” statement into the code that handles it, e.g. like this:

function onBackButtonPressed( event )  
 if event.phase == "down" and event.keyName == "back" then  
 -- handles down press of back-key (do nothing, but tell Android that you handled the key)   
 return true  
 elseif event.phase == "up" and event.keyName == "back" then  
 -- do something here, like go to you main-screen and again tell Android you handled the key  
 return true  
 end  
end  
  
Runtime:addEventListener( "key", onBackButtonPressed )  

Best,
Andreas

[import]uid: 107675 topic_id: 21992 reply_id: 120294[/import]

Hi sXc,

I guess the problem of yours just occurs because the “return true” statement tells Android that you handled all possible buttons.

If you only want to handle the “back” button you need to move the “return true” statement into the code that handles it, e.g. like this:

function onBackButtonPressed( event )  
 if event.phase == "down" and event.keyName == "back" then  
 -- handles down press of back-key (do nothing, but tell Android that you handled the key)   
 return true  
 elseif event.phase == "up" and event.keyName == "back" then  
 -- do something here, like go to you main-screen and again tell Android you handled the key  
 return true  
 end  
end  
  
Runtime:addEventListener( "key", onBackButtonPressed )  

Best,
Andreas

[import]uid: 107675 topic_id: 21992 reply_id: 120294[/import]