Javascript String.fromCharCode案例敏感性? [英] Javascript String.fromCharCode Case Sensitivity?
问题描述
我只是在监听输入元素的键盘事件,并将结果收集到一个字符串中,如此
I am simply listening for a keyup event of an input element and gather the results into a string like so
word=word+String.fromCharCode(key.keyCode);
问题是这个单词是大写字母,而我希望它区分大小写。
例如,如果我输入abcef,我累积的单词变为'ABCEF'。
The problem is that the word is in capital letters while I want it to be case-sensitive. For example if I type abcef my accumulated word becomes 'ABCEF' .
注意 - 我需要一个纯粹的javascript解决方案(没有库..)
有什么想法吗?
Note - I need a pure javascript solution (no libraries..) Any thoughts?
推荐答案
keyup
和<$ c $等事件c> keydown 将为 a
和 A
返回65(以及 true
for event.shiftKey
如果该键被按下。
Events like keyup
and keydown
will return 65 for both a
and A
(and also true
for event.shiftKey
if that key is held down).
keypress
事件返回大写和小写字母的不同密钥代码,因此为了使这个工作区敏感,你应该使用 keypress
事件, fromCharCode()
将返回正确的字母,区分大小写。
The keypress
event returns different keycodes for upper and lower case letters, so to get this working case sensitive you should use the keypress
event, and fromCharCode()
will return the correct letter, case sensitive.
这篇关于Javascript String.fromCharCode案例敏感性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!