bhylu.com

Advertisements

Keyboard Events

Keyboard events include a key value. Use input for ordinary text changes and keydown when a specific key has a clear action.

Where to run this

This example changes a document. Open it in the HTML playground, then select Run code. It runs inside a separate preview.

A practical example

HTML + JavaScript

<label for="search">Topic</label>
<input id="search">
<p id="message"></p>
<script>
  document.getElementById("search").addEventListener("keydown", event => {
    if (event.key === "Enter") document.getElementById("message").textContent = "Search requested";
  });
</script>

What happens

Pressing Enter in the field shows a message.