-3

I want to use the numpad keycodes in my webpage, but there are already predefined functions for the numpad keys.

For example, numpad key 4 referes to the keycode actions for the left arrow instead of just doing nothing. So I am just searching for optional some javascript code which deactivates those functions and allows numpad inputs to "other functions"/keycodes.

It would be great if there would be something like this:

window.allownumpad = false;

I already tried:

addEventListener("keydown", function (e) {
   e.preventDefault();
}, false);

I thought about making a javascript canvas - game with 2 players... So the controls would be like: first player [WASD] and second: [n4n8n5n6] (Please no answers like: "why dont you use the arrows?!"). Thank you for reading/answering! :)

1 Answers1

0

you can detect if the user press the 4,5,6,8 on NumPad and do your action

document.addEventListener('keydown', function (e) {
   e.preventDefault();
   console.log(e.keyCode);
   var code = e.keyCode;
   // NumPad 4
   if (code === 100) {

   }
   // NumPad 5
   else if (code === 101) {

   }

   // NumPad 6
   else if (code === 102) {
    
   }
   // NumPad 8
   else if (code === 104) {
    
   }
  })
Johnson Lee
  • 179
  • 3
  • Thanks for your research, but thats exactly the code i already made. Other controls, like WASD or Arrows are just working fine. Maybe there are different keybehaviors when using other browsers. – Konstantin.H Jul 26 '19 at 21:58