本文介绍了如何使用Keydown事件在多种语言/键盘布局中获取正确的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jquery中使用keydown事件,但是问题对于输入语言来说是无状态的.无法确定输入是英语还是希伯来语或阿拉伯语...?它仅返回键码,而我无法获得字符.

I use keydown event in jquery, but the problem it's stateless for input language. It's can't determine the input is in English language or in Hebrew or Arabic...?it returns only keycode, and I can't get character.

有什么解决办法吗?

推荐答案

要确定实际字符,应使用 keypress 事件而不是keydown. keydown为您提供键码,而keypress则指示用户输入的字符.

To determine the actual character, you should use the keypress event instead of keydown. While keydown provides you with a key code, keypress indicates the character which was entered by the user.

另一个区别是,当用户按下并按住一个键时,只会触发一次keydown事件,但是会为每个插入的字符触发单独的keypress事件.

Another difference is that when the user presses and holds a key, a keydown event is triggered only once, but separate keypress events are triggered for each inserted character.

以下是使用keypress事件的示例:

Here's an example of using the keypress event:

<body>
<form>
  <input id="target" type="text" />
</form>

<script src="http://api.jquery.com/scripts/events.js"></script>
<script>
  $("#target").keypress(function(event) {
    var charCode = event.which; // charCode will contain the code of the character inputted
    var theChar = String.fromCharCode(charCode); // theChar will contain the actual character
  });
</script>
</body>

这篇关于如何使用Keydown事件在多种语言/键盘布局中获取正确的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:16