使用 javascript 检测键盘布局
有没有办法使用 JavaScript 检测当前的键盘布局?我发现这个, 但它只检测访问者是否使用英文布局。我需要知道字符串的确切布局,例如de-ch
、fr
或en
。
Is there any way to detect the current keyboard layout using JavaScript? I found this,
but it only detects if the visitor is on the english layout. I need to know the exact layout as a string, e.g.de-ch
, fr
or en
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这个问题很老了,但是对于那些因某些键映射到其他语言环境中的不同字符而感到困惑的人,这里有一种可以用来处理它的方法。
最有可能的是,您的键盘事件处理程序与 keyup 或 keydown 事件相关联;在这种情况下, event.which 或 event.keyCode 属性的结果与用户按下的键相关联。例如,您用来输入“;”的键相同EN 布局中的键是用于在 RU 布局中键入“ж”的键,事件报告 186 作为它们的代码。
但是,如果您对按键产生的字符更感兴趣,则应该使用“keypress”事件。此事件在 keydown/up 之后触发,并报告在 event.which 或 event.charCode 中键入的字符的实际 unicode 代码点,在上面的示例中为十进制 1078 或 U+0436。
因此,如果您不想猜测键盘布局并且不需要区分 keyup/keydown,那么 keypress 可能是您的可行替代方案。它并不完美(大多数浏览器不会一致地报告不打印字符的按键上的按键事件,例如修饰键、功能键和导航键),但在处理可打印字符时,它比猜测键码要好。
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.
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.
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.
键盘布局没有标准化的标识符。它们的名称是由布局创建者指定的。它们可能具有语言关联作为一种定义的属性。
键盘布局不应与语言或区域设置标识符混淆。特别是,不存在单一的“英语布局”,而是有数十种可用于英语的布局。
我认为系统通常不会通过 JavaScript 读取当前的布局设置。
因此,无论您试图通过检测键盘布局来解决什么问题,都需要采用不同的方法。
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.
So whatever problem you are trying solve by detecting the keyboard layout, a different approach is needed.
您现在可以使用 Navigator 界面返回的 Keyboard 对象。
该对象提供对检索键盘布局图和从物理键盘切换按键捕获的功能的访问。
请参阅 https://developer.mozilla.org/en-US/docs/ Web/API/键盘
目前此功能处于实验阶段,仅适用于某些主要浏览器。
You can now use the Keyboard object returned by the Navigator interface.
This object provides access to functions that retrieve keyboard layout maps and toggle capturing of key presses from the physical keyboard.
See https://developer.mozilla.org/en-US/docs/Web/API/Keyboard
At this moment this feature is experimental and working only on some major browsers.
我对这个主题的了解以及已经实现的解决方案就是检测非英语布局键盘,即使用英语以外的任何字母的语言。解决方案是依赖正则表达式来匹配任何单词字符
\w
、任何数字字符\d
以及一系列预定义的英文键盘特殊字符,因此任何其他字符,例如由于阿拉伯语、日语等不匹配,所有这些都使用 keydown 事件,并且我正在使用此解决方案来限制和通知登录中的用户名和密码字段输入形式。我将其打包在如下函数中:然后从事件中调用该函数,如下所示:
这是 为未知投票者提供工作演示!
注意:
在正则表达式模式中,
re
您可以添加任何缺少的特殊字符,例如#、`、| 等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 usernames and passwords fields entry in login form. I packaged it in a function like the following:Then call the function from the event as the following:
This is Working DEMO for unknown down voters!
Notice:
In the regex pattern,
re
you are able to add any missing special characters such as #,`,|, etc你要找的是如何检测他们的Locale。在 HTTP 标头中,它存储在 Accept-Language 标头,但浏览器无法通过纯 JS 访问该标头。
有一个名为“浏览器语言”的 jQuery 插件,它可能会让您走上正确的道路。
You are looking for is how to detect their Locale. In the HTTP headers this is stored in the Accept-Language header, but thats not available to the browser through pure JS.
There's a jQuery plugin called 'Browser Language' that might get you going on the right path.
您可以使用 event.code 确定任何布局中的实际键位置,请尝试 http://keycode.info/
you can use event.code to determine the actual keyposition from any layout, try it http://keycode.info/
我过去使用过 Chrome I18N API,它适用于某些案例。
更具体地说,通过使用
detectLanguage()
您可以获得可靠的结果。以下代码从给定字符串中检测最多 3 种语言,并将结果显示为由换行符分隔的字符串。
I used the Chrome I18N API in the past, it works in some cases.
More specifically, by using
detectLanguage()
you can get a reliable result.The following code detects up to 3 languages from the given string and displays the result as strings separated by new lines.
如今,这被用于指纹技术。
firefox
不会公开这一点,但是基于chromium
的浏览器,是的。 (边缘、勇敢、铬、铬、歌剧
...)Nowadays, this is used in fingerprinting techniques.
firefox
don't expose this, butchromium
based browser, yes. (edge, brave, chrome, chromium, opera
...)