'How to get the correct language and case for key press in JS event listener?

I have the following jQuery event listener to get the key pressed in a text box:

$("#theInput").keyup(function (e) {
    var charCode = e.keyCode || e.which;
    alert(charCode);
    /* rest of the code */
});

My problem is how to accurately get the code for the letter typed. For instance, if I press the a key it will alert '65' - regardless of whether it was a or A. If I switch the input language to a foreign language (e.g. Japanese, Chinese, Thai) and press the a key, it still returns '65' rather than a different code to let me know the foreign character.

Is there a way to I fix this so it gives the correct language and case of the letter?



Solution 1:[1]

Keys are the same regardless of the language. You could get the case with .shiftKey, but the most reliable way is to save the actual characters:

$('#input').keyup(function(e){
  var character = this.value.slice(-1)
  console.log(character)
})

This is overly simplified, though, as text can be typed somewhere other than the end. You'd have to find the cursor position and then get the respective char.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricardo Tomasi