javascript - How can an element inside an iframe capture a key event? - Stack Overflow

I have a canvas element in a simple html document with an attached keydown and keyup listener. This wor

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?

Share Improve this question edited Mar 15, 2022 at 18:49 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 21, 2011 at 22:17 MariusMarius 59.1k35 gold badges136 silver badges151 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 1

I 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745443010a4627916.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信