Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a scenario where first i need to disable keyboard arrow keys and after some processing again Enable it,for this i write this jquery function

JavaScript
function DisableArrowKeys() {
        var ar = new Array(37, 38, 39, 40);
        $(document).keydown(function(e) {
            var key = e.which;
            if ($.inArray(key, ar) > -1) {
                e.preventDefault();
                return false; 
           }
            return true;
        });
    }


this function can disable arrow keys,after some processing i need to enable arrow key for this i made changes in the function like below

JavaScript
function EnableArrowKeys() {
        var ar = new Array(37, 38, 39, 40);
        $(document).keydown(function(e) {
            var key = e.which;
            if ($.inArray(key, ar) > -1) {              
                return true;
            }           
        });
    }


But when we call that function it does not enable arrowkeys.Please advice ...
Posted
Updated 18-Sep-11 21:18pm
v2
Comments
rkthiyagarajan 19-Sep-11 3:15am    
Use Pre Tag...
koolprasad2003 19-Sep-11 3:51am    
If it is solved make it RESOLVEDm by accepting one of the solution.

Yes you can disabled them.
37, 38, 39, 40 are the keyCodes for arrow keys.
Paste following code under <Script> tag.

C#
document.onkeydown = function(ev)
{
   var key;
   ev = ev || event;
   key = ev.keyCode;
   if(key == 37 || key == 38 || key == 39 || key == 40)
  {

     ev.cancelBubble = true;
     ev.returnValue = false;
  }

}
 
Share this answer
 
Hi ,

Here am providing some code for disabling arrow keys try these lines
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language ="javascript">
        function disfun() {
            document.onkeydown = function () {
                if (parseInt(event.keyCode) >= 37 || parseInt(event.keyCode) <= 40) {
                    return false;
                }
                else {
                    return true;
                }
            };
        
        }
     </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input type="button" id="disbtn" value ="disable" onclick ="return disfun()" />
      <input type ="text" id="txtgfg" />
    </div>
    </form>
</body>
</html>


I hope it can work for you also.


All the best
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900