Wednesday, November 19, 2003

onKeyUp and Key Listener woes

Well I was tearing my hair trying to figure out why an onKeyUp handler in a component I was building was firing twice.

To be more exact it was firing twice "sometimes", you could even hit the same key 10 times and it was ok then all of a sudden it would start firing twice and stay that way for a while, then all of a sudden stop again.

In the end I figured out the cause of the problem though not why it was random, and it is a useful one to know as you may do this yourself without seeing it as a problem.

In the onFocus handler for my component I called Key.addListener(this) so that events would go to my onKeyUp handler, and then removed the Key Listener in the onKillFocus handler.

BUT movieclip(which is the base class for all components) also has the old onKeyUp property which will fire WHEN THE MOVIECLIP IS FOCUSED so when I created onKeyUp = function(){} I then had two possible triggers for the event, one from the Key listener and one from the movieclip itself and it appears that sometimes both can fire for the same underlying event

So the lesson here is dont do Key.addListener(this) if "this" is itself a movieclip that will be focused, what you should do is create a seperate object that will recieve the onKeyUp events and then have that object callback to methods on your component that are not named the same as the standard movieclip properties




No comments:

Post a Comment