Svelte Patterns

Svelte Keydown

Detecting keypress events is useful for creating webapps and websites that feel like native applications. The good news is Svelte makes it super easy to do this.

<script>
  let keyPressed = ''
  function handleKeyUp(event) {
    keyPressed = event.key
  }
</script>

<svelte:window on:keyup={handleKeyUp} />

<h1>{keyPressed}</h1>