문제

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
도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top