Quantcast
Channel: 2D Latest Topics
Viewing all articles
Browse latest Browse all 623

[ Canvas ] keyboard input activ only on canvas element

$
0
0

Hello people !

I'm working on a project code in javascript without using any framework and I struggle with keyboard input. I would like to disable window (or document, I don't know the difference) keyboard detection while I'm in game, because the webpage move each time I use arrows keys to move my player. I already try to use getElementByID('myCanvas').onkeydown but it doesn't work at all ! Here a part of my code :

/* In a class name Engine() */

document.onkeydown = checkKey;

   function checkKey(e) {
      var keysMap = {
        37 : [-1,0],
        38 : [0,-1],
        39 : [1,0],
        40 : [0,1],
      };

       if(e.keyCode>=37 && e.keyCode<=40){
         player.setSprite(player.spriteDict[e.keyCode]) 
         game.moveTo(keysMap[e.keyCode][0],keysMap[e.keyCode][1]);
       }
       else if(e.keyCode == 82){
           game.restart();
       }
       else if (e.keyCode == 85){
         game.undo();
       }
       current.drawBoard();
   }

 

For more, see the project on Github https://github.com/KrankerApfel/Sokoban-Level-Generator


Viewing all articles
Browse latest Browse all 623

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>