Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

javascript - How do I use the Keydown event to get the correct character in multiple languages/keyboard layouts?

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.

Is there any solution??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...