0

This question says the only way for an iFrame to capture keyboard events is when it has focus, but the goal is a little different.

Our page embeds multiple iFrames, all of whom monitor the same keydown events.

Is it possible to capture keydown events for all these child iFrames from the root document? This way, we can avoid code reduplication.

If not, is the only option to capture the keydown event in each child iFrame and push the event and handling up to the root document?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • Just add these listeners on top window and when triggered, check which child iframe has a focus.. – bigless Mar 23 '19 at 02:57
  • @bigless thanks. could you post as an answer? then you can get credit. :) – Crashalot Mar 23 '19 at 04:34
  • @bigless this didn't work. if a child iframe has focus, it eats the keydown event. the parent window event handler never gets triggered. – Crashalot Mar 23 '19 at 07:23
  • Yea, now when I think about it, I'm pretty sure its not possible (atleast if iframes are cross origin) cos you would be able to steal login credentials etc. – bigless Mar 23 '19 at 11:45

1 Answers1

0

It doesn't seem possible. We decided to catch keydown events in each child iframe and pass them to the parent document where a master function processes all keyboard events.

Unfortunately, it repeats some code in each child iframe, but this seems unavoidable until a better solution emerges.

Crashalot
  • 33,605
  • 61
  • 269
  • 439