I have a canvas element in a simple html document with an attached keydown
and keyup
listener. This works correctly when I load the document in the browser and start pressing the keys. However, if I load the document in an iframe, nothing happens when I press the keys. It seems the key events never enter the iframe or the document inside the iframe. Is there a way to fix this?
I have a canvas element in a simple html document with an attached keydown
and keyup
listener. This works correctly when I load the document in the browser and start pressing the keys. However, if I load the document in an iframe, nothing happens when I press the keys. It seems the key events never enter the iframe or the document inside the iframe. Is there a way to fix this?
- What element were you focused on when pressing keys? iFrames can only listen to events in themselves. – Digital Plane Commented Sep 21, 2011 at 22:19
- Code? Example? jsfiddle? – Jared Farrish Commented Sep 21, 2011 at 22:21
2 Answers
Reset to default 1I found the answer in this question. Setting the focus on the document inside the iframe solves the problem
Setting focus to iframe contents
I found subscribing the event to window.top worked for me, as my game was several iframes deep.