【问题标题】:Convert key code to character将键码转换为字符
【发布时间】:2012-09-04 09:40:44
【问题描述】:

为什么有些键码会转换成其他字符?

http://jsfiddle.net/h9268/

我认为 219 应该是 [ 字符。

onKeyPress I get 91
onKeyDown I get 219

firefox,测试页:http://www.asquare.net/javascript/tests/KeyCode.html

更新:混淆是由 onKeyPress 和 onKeyDown 事件的键码产生的。 onKeyPress 返回字符码(91),onKeyDown 返回键码(219)。

此表同时显示 ascii 代码和密钥代码(按浏览器)。 http://unixpapa.com/js/key.html

【问题讨论】:

标签: javascript


【解决方案1】:

这是针对 Unicode 的。它采用 Unicode 值并将其转换为 Unicode 字符。

【讨论】:

    【解决方案2】:

    转换成Unicode,这里Here是转换表,用91代替

    【讨论】:

      【解决方案3】:

      W3Schools 说:

      Definition and Usage
      
      The fromCharCode() method converts Unicode values into characters.
      
      Note: This is a static method of the String object, and the syntax is always String.fromCharCode().
      

      219 必须是 91 而不是 [ 字符

      219 表示 keyCode 不是 charCode

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        • 2011-09-06
        • 1970-01-01
        相关资源
        最近更新 更多