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)">
Disabling enter key for the form keyCode === 13 || e. which === 13) { e. preventDefault(); return false; } }); If you want to prevent Enter key for a specific textbox then use inline JS code.
KeyCode was deprecated because in practice it was “inconsistent across platforms and even the same implementation on different operating systems or using different localizations.” The new recommendation is to use key or code .
If you have jQuery, try this:
$('html').bind('keypress', function(e)
{
if(e.keyCode == 13)
{
return false;
}
});
Your =
should probably be an ==
(comparison vs. assignment)
if (window.event.keyCode == 13 ) return false;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With