unable to remove onKeyEvent()

Hi.

I’m  trying to overide the back android button.

I use in almost all my scenes:

local function moveBack() native.setKeyboardFocus(nil) Runtime:removeEventListener("key", onKeyEvent) menuCreator.changeScene(2,number) end local platformName = system.getInfo( "platformName" ) local function onKeyEvent( event ) local returnValue = false if (event.phase=="up") and (event.keyName=="back") and (platformName == "Android") then timers[1]=timer.performWithDelay(100,moveBack,1) returnValue=true end return returnValue end Runtime:addEventListener( "key", onKeyEvent )

the only scene i don’t have this event is the first scene so it can return to android, all other scenes if the user pressed the back button they will go to my main menu (scene 2). scene 1 is login page.

i don’t use composer to pass scenes. i use my internal code (changeScene(futureScene,PresentSceneToDelete))

the problem is that:

Runtime:removeEventListener("key", onKeyEvent)

is not removing this listener because when i go to my login page with my login button:

local function backToStart() Runtime:removeEventListener("key", onKeyEvent) changeScene(1,number) end

in my login page the lister is still working, because if i press the back button…it will change to my menu and not to android like it supposed to do.

i even created a new listener in scene 1 with:

local platformName = system.getInfo( "platformName" ) local function onKeyEvent( event ) local returnValue=false if ( event.phase=="up")and (event.keyName == "back") and (platformName == "Android") then native.setKeyboardFocus(nil) --Runtime:removeEventListener("key", onKeyEvent) end return returnValue end Runtime:addEventListener( "key", onKeyEvent )

but this doesn’t help…the other event from my other scene fires.

I’m lost here. don’t know what i’m doing wrong. any help is appreciated.

Best regards,

Carlos.

Try declaring your onKeyEvent function ahead of actually creating it. What is most likely happening is that the current code places your onKeyEvent outside the scope of your removal code.

tried that, with no luck.

i’ve put:

local onKeyEvent

on my first line of code. still i can’t delete it…

thanks for help anyway.

Why do you want to do this?  Enable it in main.lua and leave it.

Rob

i need to check what scene I’m, so i can remove it (i’m using my internal scene creator and remover). that’s why im using it. in my main i don’t have access to that variable. i know i can do a global variable but i hate them…i avoid them like plage :slight_smile:

Have you read this tutorial?
 

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob

yes, Rob i’m using that metode alot in my scene builder and remover. all my scenes have a require “comon.lua” that share same code on all scenes, the onKeyEvent was in that file. so i only need it to put it once. the only exception is the first scene, the login. first i tried a variable passing to the function to check what scene it was, it did not work, without much thinking, then i tried removing the event and each scene create it again, still not working. guess i need to go back to basics and try again with variables and do more tests.

Generally there are two reasons why you can’t remove an event listener:

First the call’s have to be identical:

Runtime:addEventListener( “key”, onKeyEvent )

Runtime:removeEventListener( “key”, onKeyEvent )

In this case the variable onKeyEvent holds an address to a function.  That address has to be identical or you can’t remove them.   So if the value of onKeyEvent is different between the calls it won’t work.

Secondly, you can’t remove a call when you’re still in the middle of it.  This is like you can’t remove a physics object while the collision event is still going on.  Typically wrapping this in an timer.performWithDelay() will solve these things.

Finally, and while I have no hard evidence of this, people seem to have issues trying to use the key event multiple times.  I’ve never had issues when I only define it once.  It may take more work to set information about where you and use one function as opposed to trying to add and remove this event multiple times.

Rob

Try declaring your onKeyEvent function ahead of actually creating it. What is most likely happening is that the current code places your onKeyEvent outside the scope of your removal code.

tried that, with no luck.

i’ve put:

local onKeyEvent

on my first line of code. still i can’t delete it…

thanks for help anyway.

Why do you want to do this?  Enable it in main.lua and leave it.

Rob

i need to check what scene I’m, so i can remove it (i’m using my internal scene creator and remover). that’s why im using it. in my main i don’t have access to that variable. i know i can do a global variable but i hate them…i avoid them like plage :slight_smile:

Have you read this tutorial?
 

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob

yes, Rob i’m using that metode alot in my scene builder and remover. all my scenes have a require “comon.lua” that share same code on all scenes, the onKeyEvent was in that file. so i only need it to put it once. the only exception is the first scene, the login. first i tried a variable passing to the function to check what scene it was, it did not work, without much thinking, then i tried removing the event and each scene create it again, still not working. guess i need to go back to basics and try again with variables and do more tests.

Generally there are two reasons why you can’t remove an event listener:

First the call’s have to be identical:

Runtime:addEventListener( “key”, onKeyEvent )

Runtime:removeEventListener( “key”, onKeyEvent )

In this case the variable onKeyEvent holds an address to a function.  That address has to be identical or you can’t remove them.   So if the value of onKeyEvent is different between the calls it won’t work.

Secondly, you can’t remove a call when you’re still in the middle of it.  This is like you can’t remove a physics object while the collision event is still going on.  Typically wrapping this in an timer.performWithDelay() will solve these things.

Finally, and while I have no hard evidence of this, people seem to have issues trying to use the key event multiple times.  I’ve never had issues when I only define it once.  It may take more work to set information about where you and use one function as opposed to trying to add and remove this event multiple times.

Rob