Detect Key Window

I need to tell when a certain window becomes key from all other windows of the same application. I also need this to work for a HUD panel window not regular windows. I would like this to work as easily and simple as it did in Applescript Studio with just a simple method:

on became key theobject
end became key theobject

and just set every window I wanted to detect when it was key as “theobject”

Make your script a delegate of the window, and implement a handler like this:

on windowDidBecomeKey_(notif)