如何使用jquery识别键盘语言

ina*_*naz 5 html javascript css jquery

有没有办法识别键盘语言?我有一个简单的文本框.我想检查键盘语言.当用户单击文本框时,如果键盘语言与波斯语相反,则用户无法键入任何内容并显示错误:"将键盘语言更改为波斯语",当键盘语言更改时,用户可以键入.

Nas*_*efi 8

我在我的解决方案中使用了两种不同的方法来检测英语和波斯语字符:

document.getElementById('a').addEventListener('keypress',function(e){
     if (isEnglish(e.charCode))
       console.log('English');
     else if(isPersian(e.key))
       console.log('Persian');
     else
       console.log('Others')
});

function isEnglish(charCode){
   return (charCode >= 97 && charCode <= 122) 
          || (charCode>=65 && charCode<=90);
}

function isPersian(key){
    var p = /^[\u0600-\u06FF\s]+$/;    
    return p.test(key) && key!=' ';
}
Run Code Online (Sandbox Code Playgroud)
<input id="a" type="text"/>
Run Code Online (Sandbox Code Playgroud)