翻译正在进行中。

与 keypress事件不同的是,无论是否产生一个字符值,keydown事件均会被触发。

General info

Interface KeyboardEvent
Bubbles Yes
Cancelable Yes
Target Document, Element
Default Action Varies: keypress event; launch text composition system; blur and focus events; DOMActivate event; other event

Properties

Property Type Description
type 只读 DOMString The type of event.
bubbles 只读 Boolean Whether the event normally bubbles or not.
cancelable 只读 Boolean Whether the event is cancelable or not.
view 只读 WindowProxy Document.defaultView (window of the document).
detail 只读 long (float) 0.
target 只读 EventTarget (DOM element) Focused element processing the key event; root element if no suitable input element focused. (the topmost target in the DOM tree)
char 只读 DOMString (string) The character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string. See key names and char values for details.
Note: If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.
key 只读 DOMString (string) The key value of the key represented by the event. If the value has a printed representation, this attribute's value is the same as the char attribute. Otherwise, it's one of the key value strings specified in Key values. If the key can't be identified, this is the string "Unidentified." See key names and char values for details.
code 只读 DOMString (string) Holds a string that identifies the physical key being pressed. The value is not affected by the current keyboard layout or modifier state, so a particular key will always return the same value.
charCode 只读 unsigned long (int) The Unicode reference number of the key; this attribute is used only by the keypress event. For keys whose char attribute contains multiple characters, this is the Unicode value of the first character in that attribute.
Warning: This attribute is deprecated; you should use char instead, if available.
keyCode 只读 unsigned long (int) A system and implementation-dependent numerical code identifying the unmodified value of the pressed key. This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key; see Virtual key codes for a list of common values. If the key can't be identified, this value is 0.
Warning: This attribute is deprecated; you should use key instead, if available.
which 只读 unsigned long (int) A system and implementation-dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as keyCode.
Warning: This attribute is deprecated; you should use key instead, if available.
location 只读 long (float) The location of the key on the device.
repeat 只读 Boolean true if a key has been depressed long enough to trigger key repetition; otherwise, false.
locale 只读 DOMString The language code for the key event, if available; otherwise, the empty string.
ctrlKey 只读 Boolean true if the control key was down when the event was fired. false otherwise.
shiftKey 只读 Boolean true if the shift key was down when the event was fired. false otherwise.
altKey 只读 Boolean true if the alt key was down when the event was fired. false otherwise.
metaKey 只读 Boolean true if the meta key was down when the event was fired. false otherwise.

示例

This example logs the KeyboardEvent.code value whenever you press down a key inside the <input> element.

HTML

<input placeholder="Click here, then press down a key." size="40">
<p id="log"></p>

JavaScript

const input = document.querySelector('input');
const log = document.getElementById('log');

input.addEventListener('keydown', logKey);

function logKey(e) {
  log.textContent += ` ${e.code}`;
}

Result

规范

DOM L3

浏览器兼容

Starting with Gecko 25, a call of preventDefault() of keydown event prevents to dispatch following keypress event. This is valid behavior for D3E spec and the other major web browsers behave this way. On the other hand, Gecko 24 or earlier dispatched keypress event even if preventDefault() of preceding keydown event is called. Although the keypress event's defaultPrevented attribute was true in this case.

相关事件

文档标签和贡献者

标签: 
此页面的贡献者: mdnwebdocs-bot, zhangchen, BobPlus, fscholz, XHMM
最后编辑者: mdnwebdocs-bot,