Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links


How to Disable Arrow Keys in JavaScript

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 16-04-2012
Member
 
Join Date: Aug 2010
Posts: 22
How to Disable Arrow Keys in JavaScript

Sponsored Links
Hello everyone, I am planning to make online Flash game site, most of the online flash game use arrow key to play although some browser like Firefox and internet explorer etc use the arrow keys to scroll down and up page. So therefore I am trying to create code to disable arrow keys through JavaScript.i have tried one code but this doesn’t helped me to disable. I have tried the below code:

Code:
<script type="text/javascript"> 
  function KeyPressHappened(e){ 
      if (!e) e=window.event;
      var key=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; 
      if(key == 37 || key == 38 || key == 39 || key == 40) 
      { 
          e.preventDefault(); 
          return false; 
      } 
      return true; 
  } 
  document.onkeypress = KeyPressHappened;
</script>
So can anyone suggests is this code correct or what codes I need to add more on this. function Thanks in advance…

Reply With Quote
  #2  
Old 16-04-2012
Member
 
Join Date: Apr 2009
Posts: 558
Re: How to Disable Arrow Keys in JavaScript

With help of onkeydown event handler you can solve this issue. The onkeydown event handler will specifies what should occur while press any key which Document object is in focus. So I would like to try below code on your website and then you can disable the arrow up/down key:

Code:
<script type = "text/javascript">
document.onkeydown = function(ev) {	
var key;
ev = ev || event;
key = ev.keyCode;
alert (key)
if(key == 37 || key == 38 || key == 39 || key == 40) {
//e.cancelBubble is supported by IE - this will kill the bubbling process.
ev.cancelBubble = true;
ev.returnValue = false;
}
}
</script>
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Disable Arrow Keys in JavaScript"
Thread Thread Starter Forum Replies Last Post
Where are the arrow keys on the iPad Lael Portable Devices 5 25-02-2011 10:01 PM
Character not moving with Arrow keys in Actionscript 3 Ekavali Software Development 5 24-11-2010 11:31 PM
Problem of arrow keys with VMWARE D_chapple Windows Software 5 23-12-2009 10:39 AM
Arrow keys not scrolling in Internet Explorer 8 KAMAL60 Technology & Internet 3 12-11-2009 10:59 PM
Up/down left/right arrow keys not working properly Nickita in TX Vista Help 2 06-03-2008 12:48 PM


All times are GMT +5.5. The time now is 09:38 AM.