July 23, 2004
A TextField Listener Improvement

Did you ever wonder why, if you add a listener to a TextField you only receive the onChanged and the onScroller events, but not the onSetFocus and onKillFocus events? The problem is, that as the TextField is a listener itself, it cannot broadcast the onSetFocus event because this would result in an endless loop, as you can test yourself:

TextField.prototype.onSetFocus=function(oldFocus:Object):Void{
this.broadcastMessage("onSetFocus",oldFocus);
}

If you add an input field to your stage, publish it and select it, Flash will give you the "256 levels of recursion..." error.

But there is a way to work around this and it's one of the rare times that I've found use for "arguments.caller" and "arguments.callee":

TextField.prototype.onSetFocus=function(oldFocus:Object):Void{
if (arguments.caller!=arguments.callee) this.broadcastMessage("onSetFocus",oldFocus);
}

TextField.prototype.onKillFocus=function(newFocus:Object):Void{
if (arguments.caller!=arguments.callee) this.broadcastMessage("onKillFocus",newFocus);
}

What this does is to check where the onSetFocus call originated from. And only if the function was not called from the textfield itself will it propagate it to all its listeners. So if you include these lines and use addListener on a textfield you will receive all four events: onChanged, onScroller, onSetFocus and onKillFocus.

For AS1 it looks like this:

TextField.prototype.onSetFocus=function(oldFocus){
if (arguments.caller!=arguments.callee) this.broadcastMessage("onSetFocus",oldFocus);
}

TextField.prototype.onKillFocus=function(newFocus){
if (arguments.caller!=arguments.callee) this.broadcastMessage("onKillFocus",newFocus);
}

Posted at July 23, 2004 09:17 PM | Further reading
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Thank you!

Most Visited Entries
Sketches, Works & Source Code
Lectures
Contact
Backlog
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german
Impressum


My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers