Hi everyone!
I need your help with this:
When “back” key is pressed (in Android), I want to back to menu if I’m in a level scene, and I want to close the app if I’m in the menu.
This is my code:
function OnKeyEvent (event)
local KeyName = event.keyName
if (KeyName == "back") then
GoToMenu()
-- or os.exit()
return true
else
return false
end
end
Runtime:addEventListener("key", OnKeyEvent)
It doesn’t work.
Also, I tried this:
[code]local BackButtonPushed = false
function Animate (event)
if BackButtonPushed == true then
BackButtonPushed = false
GoToMenu()
– or os.exit()
end
end
local function OnKeyEvent (event)
local Phase = event.phase
local KeyName = event.keyName
if Phase == “up” and KeyName == “back” then
BackButtonPushed = true
end
return true
end
Runtime:addEventListener(“key”, OnKeyEvent)
Runtime:addEventListener(“enterFrame”, Animate)[/code]
It doesn’t work either.
I think the problem is that OnKeyEvent function doesn’t stop the back key action.
I’ve made the following test:
function OnKeyEvent (event)
local KeyName = event.keyName
if (KeyName == "back") then
local txtTest = display.newText("Back button is pushed", 100, 100, native.systemFont, 50)
return true
else
return false
end
end
Runtime:addEventListener("key", OnKeyEvent)
When I hit back key I can see txtTest, but immediately after the app is closed (or minimized).
My device is HTC Sensation with Android 4.0.
I work with build 813 for Windows.
Thanks in advance.
Rafael. [import]uid: 104648 topic_id: 26656 reply_id: 326656[/import]