Skip to content

Sequence

The Sequence component and its associated hooks provide a powerful way to handle keyboard sequences in your Svelte applications. This feature allows you to create complex keyboard sequences and trigger actions based on a series of key presses.

The sequence system consists of two main parts:

  • Sequence component: A declarative way to handle keyboard sequences
  • useSequence hook: A lower-level hook for custom sequence handling

The Sequence component provides a simple way to show/hide content based on keyboard sequences.

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence sequences="i>n>v>o>k>e>y">
<div>Got invoked!</div>
</Sequence>
PropTypeDefaultDescription
sequencesstring | string[]RequiredThe keyboard sequence(s) to detect
childrenSnippet-Content to render when sequence is detected
activeSequenceSnippet<[string | null]>-Snippet to render content with active sequence info
hideAfternumber0Time in ms after which to hide content (0 = never hide)
seperatorstring>Character used to separate keys in sequences
refHTMLElement | WindowwindowTarget element to listen for keyboard events
preventDefaultEnabledtrueWhether to prevent default browser behavior
enabledEnabledtrueWhether the sequence detection is enabled
disabledOnTagsstring[][]List of HTML tags where shortcuts should be disabled
disabledOnFormTagsbooleanfalseWhether to disable shortcuts on form elements
disabledOnContentEditablebooleanfalseWhether to disable shortcuts on contentEditable elements
scopestring*The scope this shortcut belongs to
timeoutnumber1000Maximum time (ms) allowed between key presses
ignoreModifiersbooleanfalseWhether to ignore modifier keys in the sequence

For more control over sequence handling, you can use the useSequence hook directly.

<script lang="ts">
import { useSequence } from '@invokey/svelte'
useSequence(
"i>n>v>o>k>e>y",
(event, sequence) => {
console.log('Sequence matched:', sequence)
}
)
</script>
ParameterTypeDescription
sequencesstring | string[]The sequence(s) to listen for
handler(event: KeyboardEvent, sequence: string) => voidFunction called when sequence is matched
optionsSequenceOptionsOptional configuration

The hook accepts the same options as the component, with additional sequence-specific options:

PropTypeDefaultDescription
seperatorstring>Character used to separate keys in sequences
refHTMLElement | WindowwindowTarget element to listen for keyboard events
preventDefaultEnabledtrueWhether to prevent default browser behavior
enabledEnabledtrueWhether the sequence detection is enabled
disabledOnTagsstring[][]List of HTML tags where shortcuts should be disabled
disabledOnFormTagsbooleanfalseWhether to disable shortcuts on form elements
disabledOnContentEditablebooleanfalseWhether to disable shortcuts on contentEditable elements
scopestring*The scope this shortcut belongs to
timeoutnumber1000Maximum time (ms) allowed between key presses
ignoreModifiersbooleanfalseWhether to ignore modifier keys in the sequence

Sequences are specified as strings with keys separated by the separator character (default: >). For example:

  • i>n>v>o>k>e>y
  • s>v>e>l>t>e
  • Letter keys: 'a', 'b', etc.
  • Number keys: '1', '2', etc.
  • Special keys: 'space', 'enter', 'escape', etc.
  • Modifier keys (if not ignored): 'ctrl', 'alt', 'shift', 'meta'

The simplest use case with a single keyboard sequence:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence sequences="i>n>v>o>k>e>y">
<span>Content shown when sequence is entered</span>
</Sequence>

Handle multiple keyboard sequences with the same content:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence sequences={["i>n>v>o>k>e>y", 's>v>e>l>t>e']}>
{#snippet activeSequence(sequence)}
{#if sequence}
<span>Active sequence: {sequence}</span>
{/if}
{/snippet}
</Sequence>

Show content temporarily after a sequence is entered:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence
sequences="i>n>v>o>k>e>y"
hideAfter={2000}
>
<span>Content that disappears after 2 seconds</span>
</Sequence>

Adjust the maximum time allowed between key presses:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence
sequences="i>n>v>o>k>e>y"
timeout={2000}
>
<span>Content shown when sequence is entered within 2 seconds</span>
</Sequence>

Create sequences that ignore modifier keys:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence
sequences="i>n>v>o>k>e>y"
ignoreModifiers
>
<span>Content shown when sequence is entered (ignoring modifiers)</span>
</Sequence>

Use a different separator for sequences:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence
sequences="i.n.v.o.k.e.y"
seperator="."
>
<span>Content shown when sequence is entered with dot separator</span>
</Sequence>

Listen for sequences on a specific element:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
let gameArea: HTMLElement | null = $state(null)
</script>
<div bind:this={gameArea} tabindex={-1}>
<label for="gameArea">Game Area</label>
<Sequence sequences="i>n>v>o>k>e>y" ref={gameArea}>
<span>Content shown when sequence is entered in game area</span>
</Sequence>
</div>

Show different content based on the active sequence:

<script lang="ts">
import { Sequence } from '@invokey/svelte'
</script>
<Sequence sequences={["i>n>v>o>k>e>y", "s>v>e>l>t>e"]}>
{#snippet activeSequence(sequence)}
<div class="sequence-complete">
<h2>Sequence Complete!</h2>
<p>You've entered the secret sequence!</p>
<div class="sequence-hint">
{#if sequence === "i>n>v>o>k>e>y"}
invokey Sequence Detected!
{:else}
Svelte Code Detected!
{/if}
</div>
</div>
{/snippet}
</Sequence>
  1. Choose Appropriate Timeouts: Set reasonable timeout values based on your sequence complexity and user expectations.
  2. Consider User Experience: Make sequences memorable and easy to enter.
  3. Provide Visual Feedback: Show progress or hints for longer sequences.
  4. Document Sequences: Make sure to document available sequences for your users.
  5. Handle Edge Cases: Consider what happens when users start typing sequences accidentally.
  6. Test Across Devices: Ensure your sequences work consistently across different input devices.
  7. Use Appropriate Separators: Choose separators that won’t conflict with normal typing.
  8. Consider Accessibility: Provide alternative ways to access functionality beyond keyboard sequences.
  9. Clean Up Resources: Use the enabled prop to disable sequences when components are not in use.
  10. Balance Complexity: Keep sequences long enough to avoid accidental triggers but short enough to be memorable.