Thursday, April 19, 2001

MIAW events falling through to the stage

I managed to fix yesterday's problem with capturing keydowns from a MIAW. To recap, I want to receive keydowns from a MIAW at the stage level, but I don't control the source code for the MIAW. What's a guy to do?


It turns out that if a keydownscript issues a pass command, then the keydown will automatically fall through to the stage, unless it hits and is absorbed by an editable field or text member. This is the ideal situation, since keydowns in fields are always unambiguous targets, while any others are ambiguous as to which window they may apply to. The question, then, is how to achieve this situation when you're programming the stage and you don't want to have to make changes to the MIAW code.


The answer, as is usual with Director, is to cheat.


The idea is to intervene in the MIAW and force it to exhibit this behavior - i.e., force it to issue a pass if the user isn't editing a text field in the MIAW. We do this by modifying (not setting) the MIAW's keydownscript. In a prepareframe handler, we watch the MIAW's keydownscript, and if we haven't already intervened, we insert code into it to get it to do what we want. Here's the code:

  on prepareMovie
tell window("spawned")
if (the keydownscript contains "--hook inserted" = false) then
the keydownscript = "--hook inserted" & return & /
"if (the keyboardFocusSprite > 0) then " & return & /
the keydownscript & return & /
"end if" & return & /
"pass"
end if
end tell
end prepareMovie


So, if the original keydownScript was simply a dosomething(), this code would change it to:


-- hook inserted
if (the keyboardFocusSprite > 0) then
dosomething()
end if
pass


As a result, we trap all keyboard events not directly handled by either an editable field or the function defined by the MIAW's author.