In the process of development, I faced the need to check whether a letter was pressed (English / Russian alphabet) or not.
Initially, I wanted to solve it by converting the code into a letter, but this method does not suit me, because Perhaps the Russian layout or shift / caps was included, and the letters will be displayed only in English.
For example: String.fromCharCode(#код) result when you press the "f" button, with the Russian layout will be "F", but you need "a".
|
1 answer
The character F and a have completely different character codes. https://learn.javascript.ru/keyboard-events There is a preparation for checking events, pressing the keyboard. And with keypress "a" Russian, the code is 1072, and with f - 102.
keydown keyCode=70 which=70 charCode=0 char=F keypress keyCode=1072 which=1072 charCode=1072 char=а Listen to keypress and compare characters.
- So it turns out I will have to write a checker under the entire Russian layout ?? - Maxim Korolev
- Give an example, I do not quite understand what you want to do. - DimenSi
- I need to receive not a symbol, but 1) True or False - if it is a letter or not (respectively) 2) Which letter is it specifically, and not a symbol on the keyboard - Maxim Korolev
- Then you will have to create something like a dictionary, an array with all the characters of the Russian language, and then through array.includes check the match. You can also make it even easier. Listen to keypress, get the code, transform into a symbol, then through regexp expression check whether it is in the Russian layout. Tipo /[аАЯЯЯ//.text(convertedChar) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - DimenSi
|