event issue -- What am I doing wrong?

I’m using the native keyboard to allow my user to associate a name with a high score.

There is a submit button on the screen associated with an event listener. When the user taps the submit button, the event listener looks for an “ended” event. When it receives an “ended” event, it adds the name and score to the high score table.

It seems to work fine in the simulator, but on an actual device, I get two ended events. Could this have something to do with the keyboard, since it isn’t on the simulator?

Here’s the listener code:

local function onUsername( event )  
 if ( "ended" == event.phase ) then  
\>\> On a single button press on a device, I get here twice. \<\<  
 hs\_newname = highscoreName.text  
 if(game == EASY) then  
 -- submit scores to that scoreboard.  
 else  
 --   
 end  
 native.setKeyboardFocus( nil )  
 for i = highscoreScreen.numChildren, 1, -1 do  
 local child = highscoreScreen[i]  
 child.parent:remove( child )  
 end  
 highscoreName.isVisible = false  
 highscoreScreen.isVisible = false  
 button\_enterScore:removeEventListener("touch", onUsername)  
 return true  
 end  
end  

Any ideas on how I get rid of this problem?

Thanks,

Sean.
[import]uid: 4993 topic_id: 614 reply_id: 300614[/import]

I finally figured it out.

The statement:

native.setKeyboardFocus( nil )

sends an “ended” event – I guess to the active event handler, since I hadn’t specified one for the keyboard. Obviously my code tried to handle the event, and since all the variables were still valid from the first event, the high score was added again. When the above statement got run a second time, the keyboard was already nil, so no event was sent.

An aside: this is another reason why Corona *really* needs to support the native keyboard and textbox widgets. I would have caught this a long time ago, and debugging would have been much easier if the simulator would have shown me the issues.

Sean.
[import]uid: 4993 topic_id: 614 reply_id: 1190[/import]

Hi, Sean. I was going to suggest adding an id to your button event and distinguishing it that way, but it sounds like you’ve already solved this. It also sounds like Corona’s “active event handler grabs any untargeted event” behavior may not be desirable, if it can lead to ambiguous cases like this one!

I agree with you on the inconvenience of things like the keyboard not being testable in the current simulator. We made a decision in 1.1 to deliver that feature without simulator support, because certain kinds of apps had been entirely blocked (anything involving text input) and the developers we heard from strongly preferred to have certain features sooner even without simulator support. But now that we’ve gotten that far, I do feel your pain in having a partial non-simulator workflow!

Note that the native iPhone keyboard doesn’t actually exist in Mac OSX APIs (Apple has implemented a simulation of it, but that’s proprietary to their SDK) – however, there are other ways we could better implement the simulator workflow here. For example, if there was some kind of keyboard placeholder object that possibly didn’t support fancy internationalization, etc., but DID trigger the proper Corona events you’re talking about here, that would be an obvious step forward for testing application flow. [import]uid: 3007 topic_id: 614 reply_id: 1191[/import]