addEventListener for keydown on Canvas

后端 未结 4 1854
忘了有多久
忘了有多久 2020-11-28 06:51

I am trying to make a canvas app that responds to keyboard and mouse input. I have this code:

canvas = document.getElementById(\'canvas\');
canvas.addEventLi         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 07:28

    Sometimes just setting canvas's tabindex to '1' (or '0') works. But sometimes - it doesn't, for some strange reason.

    In my case (ReactJS app, dynamic canvas el creation and mount) I need to call canvasEl.focus() to fix it. Maybe this is somehow related to React (my old app based on KnockoutJS works without '..focus()' )

提交回复
热议问题