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)"> 
2
  • 1
    answer might be around the same, but question isn't duplicate. Commented Jul 21, 2017 at 19:11
  • 1
    agree : this question is pure javascript whereas the proposed duplicate is jQuery Commented Jul 6, 2020 at 14:57

5 Answers 5

47

If you have jQuery, try this:

$('html').bind('keypress', function(e) { if(e.keyCode == 13) { return false; } }); 
Sign up to request clarification or add additional context in comments.

4 Comments

Or you can fix your code with Jason's answer. I just prefer jQuery.
Thanks for the alternative option.
I much prefer this answer and like the fact that you can replace 'html' with a more specific selector to enable more targeted disabling of the enter key (or any other key for that matter).
This works beautifully!
22

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

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

4 Comments

Good catch, I missed that one!
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 - 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: <body onkeydown="return TriggeredKey(this)">
@JasonMusgrove You seriously just saved my life. Thank you!
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.

3 Comments

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.
Yes there was. I've now fixed it. Sorry about that.
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.
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

Comments

0

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)"> 

2 Comments

You now just reversed it, the error actually was that = should've been ==
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.