In my chat application there are some text fields which gets the user login details.
When filling the user details, if a user suddenly pressed the ESC key, the data will be lost.
If need to disable the function of ESC key, which event I need to use? How can I do that.
My JavaScript code is:
function esc(e){
e = e || window.event || {};
var charCode = e.charCode || e.keyCode || e.which;
if(charCode == 27){
return false;
}
}
I searched a lot in Stack Overflow and Google; nothing worked. Please, can anyone help me to do that?
In my chat application there are some text fields which gets the user login details.
When filling the user details, if a user suddenly pressed the ESC key, the data will be lost.
If need to disable the function of ESC key, which event I need to use? How can I do that.
My JavaScript code is:
function esc(e){
e = e || window.event || {};
var charCode = e.charCode || e.keyCode || e.which;
if(charCode == 27){
return false;
}
}
I searched a lot in Stack Overflow and Google; nothing worked. Please, can anyone help me to do that?
Share Improve this question edited Mar 31, 2023 at 18:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 30, 2013 at 10:21 Human BeingHuman Being 8,38728 gold badges97 silver badges140 bronze badges 5- the escape key does nothing special per default. What do you meen by the data will be lost? You could use a keydown eventlistener to react accordingly. – Christoph Commented Jan 30, 2013 at 10:23
- Thanks for your reply.Data lost means that,the entered text in the text fields are lost. – Human Being Commented Jan 30, 2013 at 10:25
- I need that ESC will not work in my application . How to do this ? – Human Being Commented Jan 30, 2013 at 10:26
- Can you create a JsFiddle or give us a link to show what you mean? – user1636130 Commented Jan 30, 2013 at 10:30
- @Christoph nothing happened .any more ideas ? – Human Being Commented Jan 30, 2013 at 10:44
4 Answers
Reset to default 7You can bind an eventlistener to your input field to catch the Event when Esc is pressed and supress it.
document.querySelector("input").addEventListener("keydown",function(e){
var charCode = e.charCode || e.keyCode || e.which;
if (charCode == 27){
alert("Escape is not allowed!");
return false;
}
});
Example
I got the solution to control the " F5 , Esc , BackSpace(BS) " keys with the following code.
My Java Script code will be ,
document.attachEvent("onkeydown", win_onkeydown_handler);
function win_onkeydown_handler() {
switch (event.keyCode) {
case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
break;
case 27: // 'Esc'
event.returnValue = false;
event.keyCode = 0;
break;
case 08: // 'BackSpace'
if (event.srcElement.tagName == "INPUT"
|| event.srcElement.tagName == "TEXTAREA") {
} else {
event.returnValue = false;
event.keyCode = 0;
}
break;
}
}
Thanks who are all supported me to do this and for your suggestions.
I have used this for a login popup code:
jQuery(document).keyup(function(e){
if(e.keyCode==27 && popupStatus==1){
// alert('not allowed !!!');
// or any other code
return false;
}
});
I have done something similar using jquery to limit entry to numbers
$(inputBox).keydown(function(event) {
// Allow only backspace and delete
var allowed_keys = [
46, // delete
8, // backspace
];
if ($.inArray(event.keyCode, allowed_keys) != -1) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
}
});