确定 JavaScript e.keyCode 是否为可打印(非控制)字符

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

Determine if JavaScript e.keyCode is a printable (non-control) character

javascriptkeycode

提问by devios1

I'd just like to know the range(s) of JavaScript keyCodes that correspond to typeable characters; or alternatively, the range of non-typeable (control) characters like backspace, escape, command, shift, etc. so I can ignore them.

我只想知道keyCode与可键入字符对应的 JavaScript的范围;或者,不可输入(控制)字符的范围,如退格、转义、命令、移位等,所以我可以忽略它们。

The reason I ask is calling String.fromCharCode()is resulting in odd characters for control keys. For example I get "[" for left command, "%" for left arrow. Weirdness like that.

我问的原因是调用String.fromCharCode()导致控制键的奇怪字符。例如,我得到“[”代表左命令,“%”代表左箭头。那样的怪事。

回答by Shmiddty

Keydown will give you the keyCode of the key pressed, without any modifications.

Keydown 将为您提供按下的键的 keyCode,无需任何修改。

$("#keypresser").keydown(function(e){
    var keycode = e.keyCode;

    var valid = 
        (keycode > 47 && keycode < 58)   || // number keys
        keycode == 32 || keycode == 13   || // spacebar & return key(s) (if you want to allow carriage returns)
        (keycode > 64 && keycode < 91)   || // letter keys
        (keycode > 95 && keycode < 112)  || // numpad keys
        (keycode > 185 && keycode < 193) || // ;=,-./` (in order)
        (keycode > 218 && keycode < 223);   // [\]' (in order)

    return valid;
});

Only the number keys, letter keys, and spacebar will have keycodes correlating to String.fromCharCodeas it uses Unicode values.

只有数字键、字母键和空格键才会有相关的键String.fromCharCode码,因为它使用 Unicode 值。

Keypress will be the charCode representation of the text entered. Note that this event won't fire if no text is "printed" as a result of the keypress.

Keypress 将是输入文本的 charCode 表示。请注意,如果按键没有“打印”文本,则不会触发此事件。

$("#keypresser").keypress(function(e){
    var charcode = e.charCode;
    var char = String.fromCharCode(charcode);
    console.log(char);
});

http://jsfiddle.net/LZs2D/1/Will demonstrate how these work.

http://jsfiddle.net/LZs2D/1/将演示这些是如何工作的。

KeyUp behaves similarly to KeyDown.

KeyUp 的行为类似于 KeyDown。

回答by iamio

Just for background, the "keypress" event will give you a charCodeproperty whenever you press a character key.

仅作为背景,每当您按下字符键时,“keypress”事件都会为您提供一个charCode属性。

Editor.addEventListener('keypress', function(event){
    if (event.charCode) {
        //// character key
        console.log( String.fromCharCode(event.charCode) ); /// convert charCode to intended character.
    } else {
        //// control key
    }

However, the "keypress" event doesn't capture every keystroke - several keys fire before the "keypress" event.

但是,“keypress”事件不会捕获每次击键 - 在“keypress”事件之前会触发几个键。

In contrast, the "keydown" event will capture every keystroke, but it doesn't have a charCodeproperty. So how can we tell if it's a character key? Checking on every key stroke whether the keyCodeis within the lower and upper bounds for multiple ranges isn't optimally efficient. I suspect that there are also issues for characters outside of the ASCII range.

相比之下,“keydown”事件将捕获每次击键,但它没有charCode属性。那么我们如何判断它是否是一个字符键呢?检查每次击键时keyCode是否在多个范围的下限和上限内并不是最有效的。我怀疑 ASCII 范围之外的字符也存在问题。

My approach is the check the length of the event "key" property. The "key" property is an alternative to "keyCode" to determine which key was pressed. For controlkeys, the "key" property is descriptive(e.g. "rightArrow", "F12", "return", etc.). For characterkeys, the "key" property for a character key is just the character(e.g "a", "A", "~", "\", etc.). Therefore, for every character key, the length of the "key" property will have length of 1; whereas control characters will have length greater than 1.

我的方法是检查事件“key”属性的长度。“key”属性是“keyCode”的替代,用于确定按下了哪个键。对于控制键,“key”属性是描述性的(例如“rightArrow”、“F12”、“return”等)。对于字符键,字符键的“key”属性就是字符(例如“a”、“A”、“~”、“\”等)。因此,对于每个字符键,“key”属性的长度将为 1;而控制字符的长度将大于 1。

Editor.addEventListener('keydown', function(event){
    if (event.key.length == 1){ 
        //// character key
    } else {
        //// control key
    }
})

回答by Dave Zych

This article has a list of the keyCodesin Javascript:

这篇文章有一个keyCodesin的列表Javascript

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

回答by Teu

I noticed that all characters with length of 1 ('A','B',number,symbol) is printable so I use only. I use this solution for non-english characters too:

我注意到所有长度为 1 ('A','B',number,symbol) 的字符都是可打印的,所以我只使用。我也将此解决方案用于非英文字符:

if(e.key.length==1)
    print();

回答by denkquer

You can also use RegEx for this:

您还可以为此使用 RegEx:

$(".input").keyup(function (event) {
    if (event.key.match(/^[\d\w]$/i)) {
      // put function to trigger when a digit or a word character is pressed here
    }

the iflag makes the expression case insensitive.

i标志使表达式不区分大小写。