Javascript 用javascript检测键盘布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8892238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detect keyboard layout with javascript
提问by Parzifal
采纳答案by Jukka K. Korpela
Keyboard layouts do not have standardized identifiers. They have names assigned to them by the layout creator. They may have a language association as one defined property.
键盘布局没有标准化的标识符。布局创建者为它们分配了名称。它们可能具有作为一个定义属性的语言关联。
The Keyboard layouts should not be confused with language or locale identifiers. In particular there is no single “English layout” but dozens of layouts that may be used for English.
键盘布局不应与语言或区域设置标识符混淆。特别是没有单一的“英语布局”,而是有几十种布局可以用于英语。
I don't think systems normally make their current layout settings readable via JavaScript.
我认为系统通常不会通过 JavaScript 使其当前的布局设置可读。
So whatever problem you are trying solve by detecting the keyboard layout, a different approach is needed.
因此,无论您尝试通过检测键盘布局来解决什么问题,都需要采用不同的方法。
回答by Nathaniel
I know this question is old, but for those of you who have been stymied by the fact that some keys map to different characters in other locales, here is an approach you can use to deal with it.
我知道这个问题很老,但是对于那些因某些键映射到其他语言环境中的不同字符而受阻的人来说,这里有一种方法可以用来处理它。
Most likely, you have your keyboard event handler tied to a keyup or keydown event; the results of the event.which or event.keyCode property in this case is tied to which key the user pressed. For instance, the same key you use to type ';' in an EN layout is the key used to type 'ж' in an RU layout, and the event reports 186 as the code for both of them.
很可能,您将键盘事件处理程序绑定到 keyup 或 keydown 事件;在这种情况下, event.which 或 event.keyCode 属性的结果与用户按下的键相关联。例如,您用来键入“;”的同一个键 在 EN 布局中是用于在 RU 布局中键入“ж”的键,并且事件报告 186 作为它们的代码。
However, if you're more interested in the character that resulted from the key press, you should use the 'keypress' event instead. This event fires after keydown/up, and it reports the actual unicode codepoint of the character that was typed inside event.which or event.charCode, which in the example above is decimal 1078 or U+0436.
但是,如果您对按键产生的字符更感兴趣,则应改用 'keypress' 事件。此事件在 keydown/up 之后触发,它报告在 event.which 或 event.charCode 中键入的字符的实际 unicode 代码点,在上面的示例中是十进制 1078 或 U+0436。
So, if you'd prefer not to guess at keyboard layouts and you don't need to differentiate between keyup/keydown, keypress may be a viable alternative for you. It's not perfect (most browsers don't consistently report keypress events on keys that don't print characters, like modifier keys, function keys and navigation keys), but it's better than guessing at keycodes when dealing with printable characters.
因此,如果您不想猜测键盘布局并且不需要区分 keyup/keydown,keypress 可能是您的一个可行替代方案。这并不完美(大多数浏览器不会一致地报告不打印字符的键上的按键事件,如修饰键、功能键和导航键),但在处理可打印字符时,它比猜测键码要好。
回答by synthesizerpatel
You are looking for is how to detect their Locale. In the HTTP headers this is stored in the Accept-Languageheader, but thats not available to the browser through pure JS.
你正在寻找的是如何检测他们的Locale。在 HTTP 标头中,它存储在Accept-Language标头中,但浏览器无法通过纯 JS 使用该标头。
There's a jQuery plugin called 'Browser Language' that might get you going on the right path.
有一个名为“浏览器语言”的 jQuery 插件可能会让您走上正确的道路。
回答by SaidbakR
All what I know about this topic and already implemented a solution for it, is to detect non English layout keyboard, i.e Languages that uses any alphabet other than that used in English. The solution is depend on regular expression to match any word character \w
, any digit character \d
and a range of predefined English keyboard special characters, so any other characters, such as Arabic, Japanese, etc, will not match, all this uses keydown event and I am using this solution to restrict and notify usernamesand passwordsfields entry in login form. I packaged it in a function like the following:
我所知道的关于这个主题并且已经为它实现了一个解决方案的是检测非英语布局键盘,即使用除 English 之外的任何字母表的语言。解决方案是依靠正则表达式来匹配任何单词字符\w
、任何数字字符\d
和一系列预定义的英文键盘特殊字符,因此任何其他字符,例如阿拉伯语、日语等都将不匹配,所有这些都使用 keydown 事件和我我正在使用此解决方案来限制和通知登录表单中的用户名和密码字段条目。我把它打包成一个如下的函数:
function checkKeyboard(ob,e){
re = /\d|\w|[\.$@\*\\/\+\-\^\!\(\)\[\]\~\%\&\=\?\>\<\{\}\"\'\,\:\;\_]/g;
a = e.key.match(re);
if (a == null){
alert('Error 2:\nNon English keyboard layout is detected!\nSet the keyboard layout to English and try to fill out the field again.');
ob.val('');
return false;
}
return true;
}
Then call the function from the event as the following:
然后从事件中调用函数,如下所示:
$("#textFieldId").keydown(function(e){
if (!checkKeyboard($(this),e)){
return false;
}
});
This is Working DEMOfor unknown down voters!
这是不知名选民的工作演示!