17

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)">
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Cameron
  • 340
  • 1
  • 2
  • 9

5 Answers5

47

If you have jQuery, try this:

$('html').bind('keypress', function(e)
{
   if(e.keyCode == 13)
   {
      return false;
   }
});
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
22

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

if (window.event.keyCode == 13 ) return false;
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
Jason Musgrove
  • 3,574
  • 19
  • 14
  • Good catch, I missed that one! – C. Ross Aug 05 '09 at 21:08
  • I've tried this also and the enter button is still not disabled, although the rest of the keyboard is usable whereas most keys were locked before. Could there be an unknown network setting that's creating this incompatibility? I'm unfamiliar with JavaScript, but this seems like it should be simple and straightforward. – Cameron Aug 06 '09 at 15:24
  • 2
    @Cameron - apologies that this is a couple of years late ... you need to include a return statement when you specify your event handler to pass the returned value back to the event system: – Jason Musgrove Aug 15 '11 at 15:29
  • @JasonMusgrove You seriously just saved my life. Thank you! – lampwins Jan 30 '13 at 18:10
2

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.

Community
  • 1
  • 1
C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • While I couldn't get this to work yet either, is there a missing { tag? Thanks for the code! I'll keep playing with this. It seems as if something else in my code or environment isn't playing nice. – Cameron Aug 06 '09 at 16:45
  • Yes there was. I've now fixed it. Sorry about that. – C. Ross Aug 06 '09 at 17:06
  • In the second part, I find it funny that to avoid writing `document.onkeydown=handler` you end up writing `window.onload=handler`! (you know `onload=` really means `this.onload=` and `this`, here, is the `window` object). Apart from this, thanks for the code. – FrancescoMM Sep 21 '17 at 07:51
1

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)">
CJ Goldshine
  • 135
  • 1
  • 2
  • 11
  • 1
    You now just reversed it, the error actually was that = should've been == – BlueCacti Jan 23 '14 at 21:03
  • @GroundZero I thank you for the correction of my mistake and your javascript enlightenment. My mistake was due to an erroneous assumption that all commands on the line of `return false` line, were attached. The way I tested the code was by switching `return false;` with `return false; alert("!");`. However, the additional command was apparently regarded as an Else statement. – CJ Goldshine Jan 26 '14 at 00:40
1
<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

Devin
  • 33
  • 1
  • 2