JavaScript 关键代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6474811/
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-10-25 20:48:40  来源:igfitidea点击:

JavaScript Key Codes

javascriptkeycodefromcharcode

提问by Jonathan Wood

I'm working with a JavaScript routine I didn't write. It is called from a text box's onkeydownattribute to prevent unwanted keystrokes.

我正在使用一个我没有编写的 JavaScript 例程。它是从文本框的onkeydown属性中调用的,以防止不需要的击键。

The first argument is apparently not used. The second argument is a list of characters that should be allowed.

第一个参数显然没有被使用。第二个参数是应该允许的字符列表。

function RestrictChars(evt, chars) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;

    keychar = String.fromCharCode(key);

    if ((key == null) || (key == 0) || (key == 8) ||
        (key == 9) || (key == 13) || (key == 27))
        // Control key
        return true;
    else if (((chars).indexOf(keychar) > -1))
        return true;
    else
        return false;
}

This seems to work for alpha-numeric characters. However, characters such as .and /cause this function to return false, even when these characters are included in the charsparameter. For example, if the .key is pressed, keyis set to 190, and keychargets set to the "3/4" character.

这似乎适用于字母数字字符。但是,诸如.和 之类的字符/会导致此函数返回false,即使这些字符包含在chars参数中也是如此。例如,如果.按下该键,key则设置为 190,并keychar设置为“3/4”字符。

Can anyone see how this was meant to work and/or why it doesn't? I don't know enough about JavaScript to see what it's trying to do.

任何人都可以看到这意味着如何工作和/或为什么不工作?我对 JavaScript 的了解不够,无法了解它正在尝试做什么。

回答by Tim Down

Two things are wrong with that: first, if you're analysing what character has been typed, you need to use the keypressevent instead of keydownbecause that is the only event that tells you anything reliable about the actual character typed. For (a lot) more detail about about this and JavaScript key events in general, see http://unixpapa.com/js/key.html. Second, there are references to a variable called ewhich doesn't (but should) correspond with the evtparameter.

有两件事是错误的:首先,如果您正在分析键入的字符,则需要使用keypress事件而不是keydown因为这是唯一可以告诉您有关实际键入的字符的任何可靠信息的事件。有关此事件和 JavaScript 关键事件的(更多)详细信息,请参阅http://unixpapa.com/js/key.html。其次,引用了一个e不(但应该)与evt参数对应的变量。

Here's a rewrite, assuming you have a variable called textBoxthat refers to the text input element.

这是重写,假设您有一个称为textBox文本输入元素的变量。

jsFiddle: http://jsfiddle.net/9DZwL/

jsFiddle:http: //jsfiddle.net/9DZwL/

Code:

代码:

function isKeypressCharValid(e, chars) {
    e = e || window.event;

    // Allow delete, tab, enter and escape keys through
    if (/^(8|9|13|27)$/.test("" + e.keyCode)) {
        return true;
    }

    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    var charTyped = String.fromCharCode(charCode);
    return chars.indexOf(charTyped) > -1;
}

textBox.onkeypress = function(evt) {
    if (!isKeypressCharValid(evt, "abc123")) {
        return false;
    }
};

回答by Ken White

I'm not a JS person, either, but... I can explain how it's supposed to work; I don't know why you're getting the values you are for the keys you mentioned, however.

我也不是 JS 人,但是……我可以解释它应该如何工作;但是,我不知道为什么你会得到你提到的键的值。

keychar = String.fromCharCode(key);

This checks to see if the key is a printable character (letter, punctuation mark, etc.)

这将检查密钥是否是可打印的字符(字母、标点符号等)

if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
    // Control key

The above checks to see if the key is null OR (||)` 0 or 8 (backspace) or 9 (tab) or 13 (0x0D, or ENTER) or 27 (0x1B or ESCAPE) - it's exactly the Boolean result you'd expect: IF <thiscondition> or <thatcondition> or <anothercondition> or ...

以上检查键是否为空 OR ( ||)` 0 or 8 (backspace) or 9 (tab) or 13 (0x0D, or ENTER) or 27 (0x1B or ESCAPE) - 这正是你期望的布尔结果: 如果 <thiscondition> 或 <thatcondition> 或 <anothercondition> 或 ...

else if (((chars).indexOf(keychar) > -1))

This checks to see if the keycharis in the string of characters passed as the charsparameter

这将检查keychar是否在作为chars参数传递的字符串中