event.keyName problem ..

when i used this code for 1 storyboard…

yeah its functioning but my concern is…

why this ‘function’ work 2 all storyboard…  i want only 1 storyboard to worked only,

any suggestion ?

tnx advance,

local function onKeyEvent( event )

if (event.keyName == “back”) and (system.getInfo(“platformName”) == “Android”) then
clickednext()

return true
end

return false
end

Runtime:addEventListener( “key”, onKeyEvent );

When you put a listener on the global Runtime object, it’s available every where.  As long as the function onKeyEvent is still in memory it will continue to work.

I didn’t fully understand the problem, but

  1. If you only want it to work in one scene, then you have to add a:  Runtime:removeEventListener(“key”, onKeyEvent) in the exitScene() function.  Your addEventListener call should be in your enterScene() so that they can operate in pairs.

  2. If you want if functioning everywhere and it’s not, I would recommend you put the code in your main.lua and you would have to figure out what calls to make clickednext() work the way you want it to.

Rob

sir its not working… its apply 2 all scene again…

    function scene:enterScene( event )
    local group = self.view

        local function onKeyEvent( event )
        if (event.keyName == “back”) and (system.getInfo(“platformName”) == “Android”) then
          clicknext()
        return true
    end
    end

    Runtime:addEventListener( “key”, onKeyEvent )

end

function scene:exitScene( event )
    local group = self.view
    Runtime:removeEventListener(“key”, onKeyEvent)
end

Try moving the onKeyEvent function outside of enterScene.  Leave the Runtime inside.  You are running into a scoping issue.  In exitScene() it has no idea what onKeyEvent is so it’s passing a nil to the function and it can’t match up the original call.

Rob

When you put a listener on the global Runtime object, it’s available every where.  As long as the function onKeyEvent is still in memory it will continue to work.

I didn’t fully understand the problem, but

  1. If you only want it to work in one scene, then you have to add a:  Runtime:removeEventListener(“key”, onKeyEvent) in the exitScene() function.  Your addEventListener call should be in your enterScene() so that they can operate in pairs.

  2. If you want if functioning everywhere and it’s not, I would recommend you put the code in your main.lua and you would have to figure out what calls to make clickednext() work the way you want it to.

Rob

sir its not working… its apply 2 all scene again…

    function scene:enterScene( event )
    local group = self.view

        local function onKeyEvent( event )
        if (event.keyName == “back”) and (system.getInfo(“platformName”) == “Android”) then
          clicknext()
        return true
    end
    end

    Runtime:addEventListener( “key”, onKeyEvent )

end

function scene:exitScene( event )
    local group = self.view
    Runtime:removeEventListener(“key”, onKeyEvent)
end

Try moving the onKeyEvent function outside of enterScene.  Leave the Runtime inside.  You are running into a scoping issue.  In exitScene() it has no idea what onKeyEvent is so it’s passing a nil to the function and it can’t match up the original call.

Rob