js:如何在按键上获得LOCALIZED字符?

时间:2021-03-05 00:09:39

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?

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


(added sample code)

(添加示例代码)

<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>

(example online)

(在线示例)

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".

当您尝试此代码并按下ř-key时,您将在Firebug控制台中看到“代码:53;密钥:5”。我需要得到“ř”而不是“5”。

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

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

2 个解决方案

#1


15  

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);
};

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

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

#2


0  

And make sure you are using 'keypress', but not 'keydown' event.

并确保您使用'keypress',但不是'keydown'事件。

If you use 'keydown' String.fromCharCode(event.keyCode || event.which), it won't return you a localised character. At least this was an issue for me on OS X with Chrome / FF.

如果使用'keydown'String.fromCharCode(event.keyCode || event.which),它将不会返回本地化字符。至少对于OS X和Chrome / FF来说这是一个问题。

#1


15  

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);
};

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

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

#2


0  

And make sure you are using 'keypress', but not 'keydown' event.

并确保您使用'keypress',但不是'keydown'事件。

If you use 'keydown' String.fromCharCode(event.keyCode || event.which), it won't return you a localised character. At least this was an issue for me on OS X with Chrome / FF.

如果使用'keydown'String.fromCharCode(event.keyCode || event.which),它将不会返回本地化字符。至少对于OS X和Chrome / FF来说这是一个问题。