Volume keys on Android

Hi,

For some reason, the volume keys doesn’t work on Android using the latest daily build (640) when before it worked!

Cheers [import]uid: 46216 topic_id: 16545 reply_id: 316545[/import]

Hello emi,

Do when you press the volume up/down buttons, do you see a popup window showing you the volume bar going up and down? [import]uid: 32256 topic_id: 16545 reply_id: 61942[/import]

Hi Joshua,

No, I don’t see the common volume window. Probably something has changed, because I have another app built with an older version an it works perfectly.

FYI, I’m using a Samsung Galaxy S2.

[import]uid: 46216 topic_id: 16545 reply_id: 62035[/import]

emi,

Did you set up a key event listener in Lua? If you are overriding the volume keys (or all keys for that matter) by returning true, then that would block the default volume handling. For example, the below Lua code would block volume handling…

local function onKeyEvent( event )  
 return true  
end  
Runtime:addEventListener( "key", onKeyEvent );  

To work-around this, you would only return true in the above function for specific keys that you want to override, such as the back button whose default handling is to close the Corona app. [import]uid: 32256 topic_id: 16545 reply_id: 62181[/import]

Hi Joshua,

I’ve done exactly what you’ve said, returning only true if it is the back button, and it works! I must be careful when using key events :slight_smile:

Thank you for your time! [import]uid: 46216 topic_id: 16545 reply_id: 62311[/import]

Glad I could help. :slight_smile: [import]uid: 32256 topic_id: 16545 reply_id: 62420[/import]

Hi,
I am having the same problem. I am capturing the back key and returning true. But the volume key is not responding. I used logcat and the listener is picking up the ‘volume up’ or ‘volume down’ key… just not doing anything with it. I am using build 704.

Any thoughts?

here is the code:

function onBackButtonPressed(e)  
print("key: ",e.keyName)  
 if (e.phase == "down" and e.keyName == "back") then  
 downPress = true  
 else if (e.phase == "up" and e.keyName == "back" and downPress) then  
 local alert = native.showAlert( "Exit Game", "Do you really want to exit the game?.",   
 { "Cancel", "OK" }, reallyExit )  
 end  
 return true  
 end  
end  

thanks,
daf [import]uid: 58825 topic_id: 16545 reply_id: 90750[/import]

@dafwilli: Try this.[lua]function onBackButtonPressed(e)
print("key: ",e.keyName)
if (e.phase == “down” and e.keyName == “back”) then
downPress = true
else if (e.phase == “up” and e.keyName == “back” and downPress) then
local alert = native.showAlert( “Exit Game”, “Do you really want to exit the game?.”, { “Cancel”, “OK” }, reallyExit )
else
return false – if it’s not caught by the previous 2 conditions it will fall here
end
return true – if it WAS caught, we don’t want it to do its default behavior
end[/lua]Also, may I ask what the point of the downPress flag is? [import]uid: 70391 topic_id: 16545 reply_id: 90757[/import]

@Green.Castle

Thanks. That worked.

Got the code for the handling of the back button from an earlier post.

http://developer.anscamobile.com/forum/2012/02/17/disable-android-back-key

Not sure if the flag is really necessary. I will try without it.

daf [import]uid: 58825 topic_id: 16545 reply_id: 90776[/import]