Table of Contents
The onkeydown event occurs when the user presses a key on the keyboard.
You can use event.key to get the value of the key that was pressed:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Keyboard Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The keydown Event</h4>
<p>Press a key in the input field:</p>
<input type="text" id="myInput" placeholder="Type something...">
<p id="demo"></p>
<script>
document.getElementById("myInput").addEventListener("keydown", function(event) {
document.getElementById("demo").innerHTML = "Key pressed: " + event.key;
});
</script>
</body>
</html>

Result View Example
Keyboard events have the following important properties:
| Property | Description |
|---|---|
event.key |
Returns the key value (character) of the key pressed |
event.code |
Returns the physical key code (e.g. "Enter", "KeyA") |
event.altKey |
Returns true if the ALT key was pressed |
event.ctrlKey |
Returns true if the CTRL key was pressed |
event.shiftKey |
Returns true if the SHIFT key was pressed |
event.metaKey |
Returns true if the META key (Windows key / Cmd) was pressed |
You can detect if the Enter key was pressed by checking event.code === "Enter":
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Keyboard Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Detect Enter</h4>
<input type="text" id="in01" placeholder="Press Enter">
<p id="demo"></p>
<script>
const in01 = document.getElementById("in01");
in01.addEventListener("keydown", function(event) {
if (event.code === "Enter") {
document.getElementById("demo").innerHTML = "Enter was pressed!";
}
});
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.