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
102
103
|
---
title: 'Element: 键盘按下事件'
slug: Web/API/Element/keydown_event
translation_of: Web/API/Element/keydown_event
---
<div>{{APIRef}}</div>
<div></div>
<p><code><strong>keydown</strong></code>事件触发于键盘按键按下的时候。</p>
<p>与{{Event("keypress")}} 事件不同的是, 所有按键均会触发<code>keydown</code>事件,无论这些按键是否会产生字符值。</p>
<table class="properties">
<thead>
</thead>
<tbody>
<tr>
<th>Bubbles</th>
<td>Yes</td>
</tr>
<tr>
<th>Cancelable</th>
<td>Yes</td>
</tr>
<tr>
<th>Interface</th>
<td>{{domxref("KeyboardEvent")}}</td>
</tr>
<tr>
<th>Event handler property</th>
<td>{{domxref("GlobalEventHandlers.onkeydown", "onkeydown")}}</td>
</tr>
</tbody>
</table>
<p><code>keydown</code> 与 <code><a href="/en-US/docs/Web/API/Element/keyup_event">keyup</a></code> 事件捕获了键盘按键的操作,而 <code>keypress</code> 反映了具体输入某个字符的值。比如, 小写"a" 在<code>keydown</code> 和 <code>keyup</code>事件中输出的是大写A的Unicode编码65,但是在<code>keypress</code>中输出的就是小写"a"的Unicode编码97。大写 "A"在这些事件中输出的都是Unicode编码65。</p>
<p>键盘事件只能由 <code><inputs></code>, <code><textarea></code> 以及任何具有 <code>contentEditable</code> 或 <code>tabindex="-1"</code>属性的组件触发。</p>
<p>自 Firefox 65起, <code>keydown</code> 与 <code><a href="/en-US/docs/Web/API/Element/keyup_event">keyup</a></code> 事件会在IME(输入法编辑器)复合事件中被触发,目的是为了提升CJKT(中日韩台地区)用户跨浏览器性能, ({{bug(354358)}}). 若要忽略复合事件中所有 <code>keydown</code> 事件, 可以按照如下代码修改 (229是某个在IME中触发的键盘事件对应的 <code>keyCode</code>):</p>
<pre class="brush: js">eventTarget.addEventListener("keydown", event => {
if (event.isComposing || event.keyCode === 229) {
return;
}
// do something
});
</pre>
<h2 id="示例">示例</h2>
<h3 id="addEventListener_keydown_示例">addEventListener keydown 示例</h3>
<p>这个例子展示了当你在{{HtmlElement("input")}}元素中按下一个按键时, {{domxref("KeyboardEvent.code")}} 的取值 </p>
<pre class="brush: html"><input placeholder="Click here, then press down a key." size="40">
<p id="log"></p></pre>
<pre class="brush: js">const input = document.querySelector('input');
const log = document.getElementById('log');
input.addEventListener('keydown', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}</pre>
<p>{{EmbedLiveSample("addEventListener_keydown_example")}}</p>
<h3 id="onkeydown_示例">onkeydown 示例</h3>
<pre class="brush: js">input.onkeydown = logKey;</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("UI Events", "#event-type-keydown")}}</td>
<td>{{Spec2("UI Events")}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Element.keydown_event")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><code><a href="/en-US/docs/Web/API/HTMLElement/input_event">input</a></code></li>
<li><code><a href="/en-US/docs/Web/API/Element/keypress_event">keypress</a></code></li>
<li><code><a href="/en-US/docs/Web/API/Element/keyup_event">keyup</a></code></li>
<li><a href="/en-US/docs/Web/API/Document/keydown_event">Document <code>keydown</code> event</a></li>
</ul>
|