Javascript 如何使用javascript将键码转换为字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3977792/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:59:29  来源:igfitidea点击:

How to convert keycode to character using javascript

javascriptvariableskeycode

提问by faressoft

How to convert keycode to character using javascript

如何使用javascript将键码转换为字符

var key_code = 65;

result should be

结果应该是

character = "a";

回答by Skilldrick

String.fromCharCode()is what you want:

String.fromCharCode()是你想要的:

The fromCharCode() method converts Unicode values to characters.

Syntax

String.fromCharCode(n1, n2, ..., nX)

fromCharCode() 方法将 Unicode 值转换为字符。

句法

String.fromCharCode(n1, n2, ..., nX)

回答by garsax

As seen here:

这里所见:

String.fromCharCode((96 <= key && key <= 105) ? key-48 : key)