Sto provando a impostare una tastiera per una lingua straniera. Sto usando jQuery per convertire i tasti premuti in caratteri stranieri. Sto usando:Come posso acquisire altKey + i, u, e, n con jQuery?
- A - Z
- ALT + A - Z
- SHIFT + A - Z
ALT + SHIFT + A - Z
mio codice funziona per tutti questi tranne questi 4:
- ALT + ho
- ALT + U
- ALT + E
- ALT +N
Funziona bene in Firefox, ma in Chrome e Safari (sto usando un Mac) ottengo questi accenti - ˆ
, ¨
, ´
, ˜
- invece del personaggio straniero che dovrebbe entrare nella textarea.
Ecco parte del codice:
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;}
});
Questo funziona quando è e.which
65
, 68
, e 74
, ma non 73
.
Come posso farlo funzionare correttamente?
FYI, 'return false' è uguale a' e.preventDefault() 'e' e.stopPropagation() 'insieme. – VisioN
potresti fare un 'console.log' di' e.which' e controllare cosa mostra quando si fa [ALT] + [I]? Usando questo [JSFiddle] (https://jsfiddle.net/z2borwez/), funziona su Chrome per me (Windows 10) – Jordumus
@Jordumus: Sul tuo JSFiddle, ottengo 18 + 229. Quello è lo stesso codice che ottengo per [ ALT] + [u], [ALT] + [e], e [ALT] + [n]. – Travis