What key was pressed when a key event occured:
let key = event.key;
Try it Yourself »
More examples below.
The key
property returns the key that was pressed when the event occured.
The key
property is read-only.
The return value can be:
event.key
Return Value: | A String The key that was pressed: A single character ("A", "a", "4", "+", "$") Multiple characters ("F1", "Enter", "HOME", "CAPS LOCK") |
---|---|
DOM Version: | DOM Level 3 Keyboard Events. |
Write some text if the user presses the "A" key:
let key = event.key;
if (key == "a" || key == "A") {
let text = "You pressed the 'A' key!";
}
Try it Yourself »
event.key
is a DOM Level 3 (2004) feature.
It is fully supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!