addEventListener for keydown on HTML5 Canvas

JavascriptWeb DevelopmentFront End Scripts

Use the tabindex attribute on the canvas element for addEventListener on Canvas −

var myTarget, myCanvas;

window.onload = function() {
   myCanvas = document.getElementById('canvas');
   document.addEventListener('mousedown', function(event) {
      myTarget = event.target;
      alert('This is mousedown event.');
   }, false);
   
   document.addEventListener('keydown', function(event) {
      if(myTarget == myCanvas) {
         alert('This is keydown event.');
      }
   }, false);
}
raja
Published on 05-Apr-2018 14:53:16
Advertisements