Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable the function of ESC key in JavaScript?

In my chat application there are some text fields which gets the user login details.

when filling the user details,If user suddenly pressed the ESC key,the data will be lost.

I need to disable the function of ESC key ? which event I need to use ? how can I do that.

my Java Script code is ,

function esc(e){
    e = e || window.event || {};
    var charCode = e.charCode || e.keyCode || e.which;
    if(charCode == 27){
    return false;
    }
}

Searched a lot in Stack overflow and google.Nothing worked.Please any one help me to do that . Thanks..

like image 548
Human Being Avatar asked Dec 16 '22 14:12

Human Being


1 Answers

You 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

like image 84
Christoph Avatar answered Dec 18 '22 04:12

Christoph