aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/keyboardevent/which/index.html
blob: 56136f398eafa62ebcbf4900a99b414f6018de3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: KeyboardEvent.which
slug: Web/API/KeyboardEvent/which
tags:
  - Code
  - DOM
  - Key
  - KeyboardEvent
  - keyCode
  - which
translation_of: Web/API/KeyboardEvent/which
---
<div>{{ APIRef("DOM Events") }} {{Deprecated_header}}</div>

<p>{{domxref("KeyboardEvent")}} 接口的 <code><strong>which</strong></code> 只读属性返回所按下键的数字 <code>keyCode </code>或所按下字母数字键的字符代码 (<code>charCode</code>) 。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">var <em>keyResult</em> = <em>event</em>.which;
</pre>

<h3 id="返回值">返回值</h3>

<ul>
 <li><code>keyResult</code> contains the numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see {{domxref("KeyboardEvent.charCode")}} and {{domxref("KeyboardEvent.keyCode")}} for more details.</li>
</ul>

<h2 id="例子">例子</h2>

<pre class="brush: html notranslate">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;charCode/keyCode/which example&lt;/title&gt;

&lt;script type="text/javascript"&gt;

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

&lt;/script&gt;
&lt;/head&gt;

&lt;body
 onkeypress="showKeyPress(event);"
 onkeydown="keyDown(event);"
&gt;

&lt;p&gt;Please press any key.&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM3 Events','#legacy-interface-KeyboardEvent','KeyboardEvent.which')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>Initial definition; specified as deprecated</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.KeyboardEvent.which")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("KeyboardEvent")}}, the interface this property belongs too.</li>
</ul>

<div id="gtx-trans" style="position: absolute; left: 458px; top: 1890px;">
<div class="gtx-trans-icon"></div>
</div>