I'm working on a canvas-based game and am using window.addEventListener()
to attach keyup
and keydown
events globally. This works in when viewing the page normally, but when embedding it as an iframe; it doesn't get the keyup and keydown events. Is there a better way I can do this?
Asked
Active
Viewed 3,514 times
8

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Stephen Belanger
- 6,251
- 11
- 45
- 49
2 Answers
1
You can't, not unless the frame has focus.
What you can do is make a keydown on the outer window focus the iframe, or always somehow focus the iframe, or focus the iframe by default (might be good enough, not sure what you're doing)
But for the window keydown to fire on any window (frame or not) that frame needs focus.

Simon Sarris
- 62,212
- 13
- 141
- 171
1
It seems you can just do var realWindow = window.parent || window;
and use addEventListener on realWindow
instead.

Stephen Belanger
- 6,251
- 11
- 45
- 49
-
1This would only work if the outer page embedding the frame comes from the same domain (web site). – Richard Connamacher Aug 17 '12 at 17:25