KeyboardEvent.which

已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。

KeyboardEvent 接口的 which 只读属性返回所按下键的数字 keyCode 或所按下字母数字键的字符代码 (charCode) 。

  • keyResult contains the numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see KeyboardEvent.charCode and KeyboardEvent.keyCode for more details.

例子

html
<html>
  <head>
    <title>charCode/keyCode/which example</title>

    <script type="text/javascript">
      function showKeyPress(evt) {
        alert(
          "onkeypress handler: \n" +
            "keyCode property: " +
            evt.keyCode +
            "\n" +
            "which property: " +
            evt.which +
            "\n" +
            "charCode property: " +
            evt.charCode +
            "\n" +
            "Character Key Pressed: " +
            String.fromCharCode(evt.charCode) +
            "\n",
        );
      }

      function keyDown(evt) {
        alert(
          "onkeydown handler: \n" +
            "keyCode property: " +
            evt.keyCode +
            "\n" +
            "which property: " +
            evt.which +
            "\n",
        );
      }
    </script>
  </head>

  <body onkeypress="showKeyPress(event);" onkeydown="keyDown(event);">
    <p>Please press any key.</p>
  </body>
</html>

规范

Specification
UI Events
# dom-uievent-which

浏览器兼容性

BCD tables only load in the browser

See also