jQuery - 按下字符 - 无论键盘如何
我正在尝试使用 jQuery 检索插入到文本字段/输入中的字符。
我使用通常的:
var character = String.fromCharCode( e.keyCode || e.which );
方法,但当我使用我刚刚注意到的不同键盘布局时,就会出现唯一的问题。
例如,在标准美国键盘上它可以完美运行。例如,在德语键盘上,如果我将语言设置为英语 - 基本上将其渲染为标准美国键盘,当我按下相当于 :;'\,./[]=-
的字符时,我得到了我在键盘上实际看到的德语字符(尽管相应的英语字符已添加到输入中)。
示例:如果我 console.log( character )
对于以下句子,我得到:
- 在输入中:
[]\';/.,
- 在控制台中:
我的明显问题是
,如何确保获得真正的字符插入器?
I am trying to retrieve the character inserted into a textfield/input with jQuery.
I use the usual:
var character = String.fromCharCode( e.keyCode || e.which );
method, but the only problem occurs when I use a different keyboard layout which I just noticed.
So for example, on a Standard US Keyboard it works perfectly. On a German Keyboard for instanece, if I have the language set to English - basically rendering it a standard US Keyboard, when I press the characters equivalent to :;'\,./[]=-
, I get the German characters I actually see on my keyboard (although the English equivalent of then is added to the input).
Example: if I console.log( character )
for the folowing sentence I get:
- In the input:
[]\';/.,
- In the console:
ÛݺÞܼ¾¿
My obvious question is, how can I make sure to get the true character inserter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
keypress 事件与 keyup 和 keydown 事件不同,因为它包含实际按下的键。尝试使用它来代替。用这个片段检查一下:
The keypress event is different from the keyup and keydown events as it contains the key actually pressed. Try using that instead. Check it out with this snippet:
您可以做的就是使字符出现在隐藏的文本框中并获取实际值。这样,您将获得角色。您当前正在传递键代码,就像它是字符代码一样。它们不一样。
http://jsfiddle.net/RQQpT/
What you can do is making the character appear in a hidden textbox and fetch the actual value. That way, you will get the character. You are currently passing the key code as if it is a character code. They are not the same.
http://jsfiddle.net/RQQpT/