Disable keyboard <enter> key [duplicate]

I just want to disable the Enter key on the keyboard. The following script locks the whole keyboard down for some reason except for still allowing only the Enter key to be used.

If this helps pinpoint what's missing or wrong, I'm using V.S. 2005, VB.NET 2.0, and I.E. 7.

<meta http-equiv="content-type" content="text/html; charset=windows-1252">

<head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1252">

    <script language="JavaScript">
    function TriggeredKey(e)
    {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        if (window.event.keyCode = 13 ) return false;
    }
    </script>
</head>
<body onkeydown="TriggeredKey(this)">

Answers:

Answer

If you have jQuery, try this:

$('html').bind('keypress', function(e)
{
   if(e.keyCode == 13)
   {
      return false;
   }
});
Answer

Your = should probably be an == (comparison vs. assignment)

if (window.event.keyCode == 13 ) return false;
Answer

I've used this code successfully.

function handleKeypress(e){

    e = e || window.event ;
    if (e == null){
        return false;
    }

    if (e.keycode == 13){
        CompleteEvent(e);
    }
}

function CompleteEvent(e){
    e.cancelBubble = true;
    e.returnValue = false;
}

Also I highly recommend using the new form of hook setting for javascript.

function setKeyHook()
{     
    var eventName = 'onkeydown';
    var handlerFunc = handleKeypress;


    body.detachEvent( eventName, handlerFunc );              

    body.attachEvent( eventName, handlerFunc );

}

onload = setKeyHook;

Good luck.

See this question for more information than you wanted. Kudos to Peter Bailey for teaching me.

Answer

This worked for me.

<meta http-equiv="content-type" content="text/html; charset=windows-1252">

<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">

<script language="JavaScript">
function TriggeredKey(e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    if (window.event.keyCode != 13 ) return false;
}
</script>
</head>
<body onkeydown="TriggeredKey(this)">
Answer
<script type="text/javascript">

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

document.onkeypress = stopRKey;

</script> 

add script between

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.