Categories
javascript jquery keyboard-shortcuts macos

How can I capture altKey + i, u, e, n with jQuery?

I’m trying to set up a keyboard for a foreign language. I’m using jQuery to convert keys pressed into foreign characters. I am using:

  • A - Z
  • ALT + A - Z
  • SHIFT + A - Z
  • ALT + SHIFT + A - Z

    My code works for all of these except these 4:

    • ALT + I
    • ALT + U
    • ALT + E
    • ALT + N

It works fine in Firefox, but in Chrome and Safari (I’m using a mac) I get these accent marks – ˆ, ¨, ´, ˜ – instead of the foreign character that is supposed to go into the textarea.

Here is some of the code:

function type(e, char, textArea) {
e.preventDefault();
var start = textArea[0].selectionStart;
var end = textArea[0].selectionEnd;
var len = textArea.val().length;
var newPos = start + char.length;
textArea.val(textArea.val().substring(0, start) + char + textArea.val().substring(end, len));
textArea[0].setSelectionRange(newPos, newPos);
}
$('textarea').keydown(function(e) {
var textArea = $(this);
if (e.which == 65 && e.altKey) {type(e, 'अ', textArea);return false;}
if (e.which == 68 && e.altKey) {type(e, 'ड', textArea);return false;}
if (e.which == 73 && e.altKey) {type(e, 'इ', textArea);return false;}
if (e.which == 74 && e.altKey) {type(e, 'ज्ञ', textArea);return false;}
});

This works when e.which is 65, 68, and 74, but not 73.

How can I get this to work right?