javascript keyup 事件总是返回大写字母

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

keyup event always return uppercase letter

javascriptjquery

提问by Tomer

I'm trying to write something similar to a 'placeholder' polyfill. I would like to catch the keyup event on an input field and get the char the user entered, so i have this code:

我正在尝试编写类似于“占位符”polyfill 的内容。我想在输入字段上捕获 keyup 事件并获取用户输入的字符,所以我有以下代码:

 $elem.on('keyup',function(e){  
    var $this = $(this),    
        val = $this.val(),
        code = (e.keyCode ? e.keyCode : e.which);
    console.log(String.fromCharCode(code));

});

The problem is this always returns an Uppercase version of the pressed char, how can i know if the pressed char was uppercase or lowercase?

问题是这总是返回按下字符的大写版本,我怎么知道按下的字符是大写还是小写?

I know keypressgives the pressed char but it does not activate on all keypress events (like backspace).

我知道keypress给出了按下的字符,但它不会在所有按键事件(如退格键)上激活。

采纳答案by Ed Bayiates

You can't easily get this information from keyup because keyup only knows which keys are being pressed, not which letters are being typed. You could check for the shift key being down and you could also try to track caps lock yourself, but if someone pressed it outside the browser, you'll end up getting the wrong case characters.

你不能轻易地从 keyup 得到这个信息,因为 keyup 只知道按下了哪些键,而不知道正在输入哪些字母。您可以检查是否按下了 shift 键,也可以尝试自己跟踪大写锁定,但是如果有人在浏览器外按下它,您最终会得到错误的大小写字符。

You could try the keypress event., though jQuery reports it does not always behave the same cross-browser. From the doc:

您可以尝试按键事件。,虽然 jQuery 报告它并不总是在跨浏览器中表现相同。从文档:

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

请注意, keydown 和 keyup 提供了一个代码,指示按下了哪个键,而 keypress 指示输入了哪个字符。例如,小写的“a”将被 keydown 和 keyup 报告为 65,但被 keypress 报告为 97。所有事件都将大写“A”报告为 65。由于这种区别,在捕获特殊的击键时,例如箭头键,.keydown() 或 .keyup() 是更好的选择。

回答by MLeFevre

keyuponly knows the key being pressed, not additional conditions (in this case if its a lower/upper case). Try the keypressevent instead (I've used document for the example to demonstrate - change it to the required element)

keyup只知道按下的键,而不知道附加条件(在这种情况下,如果是小写/大写)。keypress改为尝试该事件(我在示例中使用了文档进行演示 - 将其更改为所需的元素)

eg:

例如:

<script type="text/javascript">

$(document).on('keypress',function(e) {  
    var $this = $(this),    
        val = $this.val(),
        code = e.keyCode || e.which;
    console.log(String.fromCharCode(code));
});

</script>

回答by clamchoda

You will need to capture your key event a onkeypress so you can access the character code. Even if you wanted to detect a capslock / shift + keystroke, it would still register as two separate keystrokes making it difficult to determine uppper vrs lower case.

您需要通过 onkeypress 捕获您的按键事件,以便您可以访问字符代码。即使您想检测大写锁定 / shift + 击键,它仍会注册为两个单独的击键,从而难以确定大写 vrs 小写。

Some important KeyCode notes:

一些重要的 KeyCode 注释:

"Be careful when accessing the keyCode property during an onkeydown or onkeyup event, as it is set whenever any key is pressed, including non character keys like "Shift". This means if you try to press "Shift+a" to try and get the keyCode for "A", you will always end up getting two keyCodes instead, one for "Shift" and one for "A" in that order. What you won't get regardless is the keyCode for "a", as keyCode always returns the unicode value of the uppercase version of a character. To derive the keyCode for "a" (lowercase), you must probe the keyCode returned during the onkeypress event in IE, and since Firefox doesn't set keyCode during onkeypress, switch to e.charCode or e.which instead for that browser."

“在 onkeydown 或 onkeyup 事件期间访问 keyCode 属性时要小心,因为无论何时按下任何键都会设置它,包括像“Shift”这样的非字符键。这意味着如果您尝试按“Shift+a”来尝试获取“A”的keyCode,你最终总是会得到两个keyCode,一个是“Shift”,一个是“A”。无论如何你都不会得到“a”的keyCode,因为keyCode总是返回字符大写版本的 unicode 值。要导出“a”(小写)的 keyCode,您必须在 IE 中的 onkeypress 事件期间探测返回的 keyCode,由于 Firefox 在 onkeypress 期间没有设置 keyCode,请切换到e.charCode 或 e.which 代替该浏览器。”

Some important CharCode notes:

一些重要的 CharCode 注释:

"Returns the character code of the key pressed during an onkeypress event. and is only set for keys in which a character is associated with it (ie: "a", "b", or "z"). Keys with no character association like "Shift" or "Ctrl" do not qualify. Other points: - Different values are returned for upper and lowercase characters (ie: "a" versus "A")."

“返回在 onkeypress 事件期间按下的键的字符代码。并且仅针对与其关联的字符(即:“a”、“b”或“z”)设置。没有字符关联的键像“Shift”或“Ctrl”不符合条件。其他要点: - 大写和小写字符返回不同的值(即:“a”与“A”)。

Here is a great source from which the two quotes came from which can most likely aid you with any further confusion.

这是两个引文的重要来源,很可能可以帮助您解决任何进一步的困惑。

Keycods and charcodes

键码和字符码

回答by Rahul

You can get the charcode on a keypress event and for "backspace",'insert' etc , you can use keydowns keyCode property.

您可以在按键事件中获取字符代码,对于“退格”、“插入”等,您可以使用 keydowns keyCode 属性。