Question

I have a problem. I want to stop some keyboard and mouse keys on my webpage through using pure JavaScript, no JQuery. How can I do this? Here is my desired keys list that I want to stop?

  • Keyboard Up Arrow
  • Keyboard Down Arrow
  • Keyboard PageUp Key
  • Keyboard PageDown Key
  • Keyboard Home Key
  • Keyboard End Key
  • Keyboard CTRL + Numpad 2 Key
  • Keyboard CTRL + Numpad 8 Key
  • Keyboard CTRL + Keyboard U Key
  • Mouse Wheel Up
  • Mouse Wheel Down
Was it helpful?

Solution 2

I found my desired code and its working. So I am also sharing here thinking that it can help you too. First see the live DEMO at http://jsfiddle.net/CQLpV/1/ then garb the code from below.

<script type='text/javascript'>
window.addEventListener("keydown", function(e) {
    // Space Bar And Arrow Keys Are Addto To Stop Working
    if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
        e.preventDefault();
    }
}, false);
</script>

You can get more keyboard keys characters codes from All Keyboard Keys Char Codes List.

OTHER TIPS

What you are going to want to do is to prevent the default action when those keys are hit by listening to the events...

document.addEventListener('keydown', function(e) {

   if(e.which == 91 /* up arrow */ || e.which == 93 /* down arrow */ ... add all the keys here)
      e.preventDefault();
      return false;
   }
}

Then just do the same for the mouse wheel char codes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top