js:如何在按键上获得LOCALIZED字符? [英] js: how to get LOCALIZED character on keypress?

查看:131
本文介绍了js:如何在按键上获得LOCALIZED字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在keypress事件上获得本地化角色。例如:在捷克键盘上我需要得到不是5个字符(键码53)(参见捷克键盘布局)。有没有其他方法来获得角色,不使用文本输入和阅读价值?换句话说,有什么方法可以从事件对象中获取关于当前用户键盘布局的字符?

I need to get localized character on keypress event. For example: on czech keybord I need to get ř not 5 character (key code 53) (see Czech keyboard layout). Is there any other way to get character and to not use text input and reading value? By other words is there any way how to get character from event object respecting current user keyboard layout?

(添加了示例代码)

<html>
<body>
<script language="JavaScript">
function onLoad() {
    console.log(document.getElementById("test"));
    document.getElementById("test").onkeydown = function(event) {
                console.log("Code: "+event.keyCode+"; Key: "+String.fromCharCode(event.keyCode));
        }
}
</script>
<body onLoad="onLoad();">
    <input id="test">
</body>
</html>

(例如 online

你试试这个代码,按下ř-key,你会在Firebug控制台中看到Code:53; Key:5。我需要得到ř而不是5。

When you try this code, and you press ř-key you will see in Firebug console "Code: 53; Key: 5". And I need to get "ř" not "5".

我认为这个问题出现在所有非英语键盘上。

I think that this "problem" appears on all non-english keybords.

推荐答案

使用 keypress 事件将为您提供键入的字符,无论键盘布局如何。

Using the keypress event will give you the character typed, regardless of keyboard layout.

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var charCode = evt.which || evt.keyCode;
    var charTyped = String.fromCharCode(charCode);
    alert("Character typed: " + charTyped);
};

这是我通常链接到Jan Wolter的优秀页面,记录了JavaScript密钥处理: http://unixpapa.com/js/key.html

Here's my usual link to Jan Wolter's excellent page documenting JavaScript key handling: http://unixpapa.com/js/key.html

这篇关于js:如何在按键上获得LOCALIZED字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆