Click here to Skip to main content
14,328,470 members
Rate this:
Please Sign up or sign in to vote.
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

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

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...
koolprasadd 19-Sep-11 3:51am
   
If it is solved make it RESOLVEDm by accepting one of the solution.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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;
  }

}
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi ,

Here am providing some code for disabling arrow keys try these lines
<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
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100