Build 569

Back and Menu buttons sample code is missing. I’m using Windows version of Corona for Android and the KeyEvents folder does not exist in the Hardware folder. Am I missing something? Or an I missing something?

I was excited and wanted to try the new feature but I cannot “See new sample Hardware/KeyEvents/main.lua”.
Glenn [import]uid: 38820 topic_id: 12404 reply_id: 312404[/import]

It’s there in the .dmg

-- Project: KeyEvents  
--  
-- Date: July 12, 2011  
--  
-- Version: 1.0  
--  
-- File name: main.lua  
--  
-- Author: Ansca Mobile  
--  
-- Abstract: Handle navigation key events on Android devices  
--  
-- Demonstrates:   
--  
-- File dependencies: build.settings  
--  
-- Target devices: Android  
--  
-- Update History: none  
--  
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license  
-- Copyright (C) 2011 ANSCA Inc. All Rights Reserved.  
---------------------------------------------------------------------------------------  
  
-----------------------------------------------------------------------  
-- Create text message label  
--  
local title = display.newText( "Press a navigation key", 0, 0, nil, 20 )  
title.x = display.contentWidth/2 -- Center the text  
title.y = 20  
  
local eventTxt = display.newText( "Waiting for nav key event...", 0, 0, nil, 20 )  
eventTxt.x = display.contentWidth/2 -- Center the text  
eventTxt.y = display.contentHeight/2-30  
eventTxt:setTextColor( 255, 255, 255 )  
  
local function onKeyEvent( event )  
 local phase = event.phase  
 local keyName = event.keyName  
 eventTxt.text = "("..phase.." , " .. keyName ..")"  
  
 -- we handled the event, so return true.  
 -- for default behavior, return false.  
 return true  
end  
  
-- Add the key callback  
Runtime:addEventListener( "key", onKeyEvent );  

Now all I need is honeycomb support! [import]uid: 12822 topic_id: 12404 reply_id: 45218[/import]

I have little problem with director. When i press back button, director changes scene, but in new scene graphics are messed up. Few images are completely white. Here is my code:

local function onKeyEvent(event)  
if event.keyName == "back" then  
Runtime:removeEventListener("key",onKeyEvent)  
director:changeScene ("menu")  
return true  
end  
end  
  
Runtime:addEventListener("key",onKeyEvent)  

Could someone help me little bit? [import]uid: 18445 topic_id: 12404 reply_id: 45227[/import]

Thank you Yobonja.

I tried the code you posted above and on my Galaxy S the event.phase are wrong. When I push the menu or back button it says up. Then when I release the button it says down.

There’s also a problem with the back key. If I press and hold the back key down for more than a second the phase will get stuck and not change (menu button does not have this problem).
[import]uid: 38820 topic_id: 12404 reply_id: 45238[/import]

The phases being backwards is filed as bug 6882. Thanks!

Is anyone else seeing the stuck back key problem? On what device(s)? [import]uid: 6787 topic_id: 12404 reply_id: 45260[/import]

I have problem with back key and director. Mentioned above. Could someone post working example how to use back key and director?
Using Acer Liquid, Android 2.3.4 [import]uid: 18445 topic_id: 12404 reply_id: 45276[/import]

snarla Thank you for filing the bug and having it fixed in build 570.

The only problem i’m still having is the stuck back key problem. [import]uid: 38820 topic_id: 12404 reply_id: 45388[/import]

Has somebody back key working with director? When i press back key, director changes scene, but some graphics are completely white. Help, please. [import]uid: 18445 topic_id: 12404 reply_id: 45389[/import]

uapo15 I had the same problem but got it working. You cannot call a scene within the back key listener. Instead I set a flag and handle it in a enterframe.

local backButtonPushed = false  
  
local function animate(event)  
 if backButtonPushed == true then  
 if fxVolumeOn == true then audio.play(clickSound) end  
 director:changeScene("levelselect1")  
 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  

Hope this helps you.

Glenn [import]uid: 38820 topic_id: 12404 reply_id: 45391[/import]

THANKS! Got it working now! [import]uid: 18445 topic_id: 12404 reply_id: 45392[/import]

I got new problem: glennbjr code works, but it is someway global.
When i first time press back key in “about” scene, director changes back to “menu” scene. Thats ok, but after that, in every other scene director changes to “menu” scene. And that happens even in scene where i dont have that glennbjr back key code.
I have removed listeners in every time director changes scene. And now i cant close my game in main menu with pressing back key. I can only quit my game pressing home key, but then my game is still running in background.
I am little bit confused. Could someone help me?
[import]uid: 18445 topic_id: 12404 reply_id: 45406[/import]

Sounds like an error somewhere in your code. I’m able to switch between all my scenes without any problems.

To exit my game from the main menu I handle the back button, drop down a menu with “Exit Game?” with yes or no buttons, if yes is pushed then exit game with os.exit().

I’m using an older version of Director with its cleanup function. Director Beta 1.3 or earlier should work with the code below. Here’s a complete scene of how the back button can be handled in a director scene. I hope the code is not too long for this post :o|

[code]module(…, package.seeall)


– GROUPS

local localGroup = display.newGroup()


– BACK BUTTON FLAG

local backButtonPushed = false


– LISTENERS

local function animate(event)
if backButtonPushed == true then
backButtonPushed = false
os.exit()
– or
–director:changeScene(“menu”)
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


– INIT VARS

local function initVars ()


– Listeners

Runtime:addEventListener( “key”, onKeyEvent )
Runtime:addEventListener( “enterFrame”, animate )

end


– CLEAN

function clean ( event )
Runtime:removeEventListener( “key”, onKeyEvent )
Runtime:removeEventListener( “enterFrame”, animate )
backButtonPushed = nil
end


– NEW

function new()


– Initiate variables

initVars()


– MUST return a display.newGroup()

return localGroup

end
[/code]
[import]uid: 38820 topic_id: 12404 reply_id: 45416[/import]

Thanks for you time. I created new project based on your code above and it worked perfectly. So i have error somewhere in my code. Going to recreate my game based to code above. Hope that it will start working after that. [import]uid: 18445 topic_id: 12404 reply_id: 45436[/import]

Got it working now! I dont know why it didnt work before, but now it just works. Big thanks for your time glennbjr! [import]uid: 18445 topic_id: 12404 reply_id: 45443[/import]