简体   繁体   English

如何使用 jquery 识别键盘语言

[英]How to identify what keyboard language is with jquery

is there any way to identify the keyboard language?有没有办法识别键盘语言? i have a simple textbox.我有一个简单的文本框。 that i want to check keyboard language.我想检查键盘语言。 when the user click on textbox if the keyboard language was Contrary to Persian the user can't type anything and show the error:" change your keyboard language to persian" and when the keyboard language changed user can type.如果键盘语言与波斯语相反,当用户单击文本框时,用户无法键入任何内容并显示错误:“将键盘语言更改为波斯语”,并且当键盘语言更改后,用户可以键入。

I have used two different approaches for detecting English and Persian characters in my solution: 我在我的解决方案中使用了两种不同的方法来检测英语和波斯语字符:

 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 = /^[\؀-\ۿ\\s]+$/; return p.test(key) && key!=' '; } 
 <input id="a" type="text"/> 

I don't think that's possible - anyway it's probably not what you want (Caps Lock, for example, will still output English) I'd recommend placing a keypress event listener on your textarea and checking each letter against a "Persian only" regex like this (untested): 我不认为这是可能的 - 无论如何它可能不是你想要的(例如,Caps Lock仍会输出英文)我建议在你的textarea上放置一个keypress事件监听器并检查每个字母对“仅限波斯语”的正则表达式像这样(未经测试):

 document.getElementById('a').addEventListener('keypress',function(e){ if (e.charCode > 160) console.log('persian'); else console.log('english'); }); 
 <input type="text" id="a"/> 

If you want to match ZERO WIDTH SPACE you should add this too: 如果你想匹配ZERO WIDTH SPACE,你也应该添加它:

\u200B

So 所以

var p = /^[\u0600-\u06FF\u200B\s]+$/;

for more characters like ” | 更多字符如“| « | «| » | »| ?| ?| ; ; | | : | :| ... see Regex Persian Language ...见正则表达式波斯语

This: 这个:

function only_persian(str){
    var p = /^[\u0600-\u06FF\s]+$/;
  if (!p.test(str)) {
        alert("not format");
    }
}

or you can use this: http://imanmh.github.io/persianRex/ 或者你可以使用这个: http//imanmh.github.io/persianRex/

using jquery :使用 jquery :

$("#selector").keyup(function (event) {
    var code = event.key.charCodeAt(0);
    if (isEnglish(code)) {
        alert("you typed in english");
        return;
    }
    else {
        alert("please type in english");
    }
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM