如何禁用 JavaScript 中的 ESC 键功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14601616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:21:20  来源:igfitidea点击:

how to disable the function of ESC key in JavaScript?

javascriptjavascript-events

提问by Human Being

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.

填写用户详细信息时,如果用户突然按下ESC键,数据将丢失。

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

我需要禁用ESC键的功能吗?我需要使用哪个事件?我怎样才能做到这一点。

my Java Script code is ,

我的 Java 脚本代码是,

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..

在堆栈溢出和谷歌中搜索了很多。没有任何效果。请任何人帮助我做到这一点。谢谢..

采纳答案by Human Being

I got the solution to control the " F5 , Esc , BackSpace(BS) " keys with the following code.

我得到了使用以下代码控制“ F5 , Esc , BackSpace(BS) ”键的解决方案。

My Java Script code will be ,

我的 Java Script 代码将是,

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.

感谢所有支持我这样做的人以及您的建议。

回答by Christoph

You can bind an eventlistener to your input field to catch the Event when Escis pressed and supress it.

您可以将事件侦听器绑定到您的输入字段以在Esc按下时捕获事件并抑制它。

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

例子

回答by Riju Mahna

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;
    }
});

回答by Dave Gill

I have done something similar using jquery to limit entry to numbers

我使用 jquery 做了类似的事情来限制输入数字

    $(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(); 
            }   
        }
    });