Skip to content

Vue Example: Keyboard Layouts

<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import {
  HotkeysProvider,
  useFormatForDisplay,
  useHotkey,
  useKeyboardLayout,
} from '@tanstack/vue-hotkeys'
import { HotkeysDevtoolsPanel } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'
import OwnLayout from './OwnLayout.vue'

const plugins = [{ name: 'TanStack Hotkeys', component: HotkeysDevtoolsPanel }]

// Labels physical keys such as `[KeyW]` with the character the user's
// keyboard layout types there, once the browser reports the layout.
const formatForDisplay = useFormatForDisplay()
// The browser's layout as a map from physical code to character, such as
// KeyQ to 'a' on French AZERTY. `null` until it loads, and always `null` in
// Firefox and Safari, which don't report a layout.
const layoutMap = useKeyboardLayout()
const germanFired = ref('none')
const everyLayoutFired = ref('none')
const physicalFired = ref('none')

// These type-check because German keys are registered in index.ts.
useHotkey('Mod+Ö', () => (germanFired.value = 'Toggle comment'))
useHotkey('Mod+Shift+Ä', () => (germanFired.value = 'Add annotation'))
useHotkey('ß', () => (germanFired.value = 'Toggle sidebar'))
useHotkey('§', () => (germanFired.value = 'Show sections'))

// A to Z, 0 to 9, named keys, and physical codes type-check under every
// registration. Letters match by the character typed, so Mod+K fires on the
// key labeled K on any Latin layout.
useHotkey('Mod+K', () => (everyLayoutFired.value = 'Open command palette'))
useHotkey('Mod+Enter', () => (everyLayoutFired.value = 'Send'))
useHotkey('Escape', () => (everyLayoutFired.value = 'Dismiss'))

// A physical code in brackets matches a key position, not a character.
// `[KeyW]` is the key at the US W position, which types Z on French AZERTY,
// so WASD movement stays in the same place on every keyboard.
useHotkey('[KeyW]', () => (physicalFired.value = 'Move up'))
useHotkey('[KeyA]', () => (physicalFired.value = 'Move left'))
useHotkey('[KeyS]', () => (physicalFired.value = 'Move down'))
useHotkey('[KeyD]', () => (physicalFired.value = 'Move right'))
// Mod+Z would undo in a focused text field. The US Z position types Y on
// German QWERTZ, so the label reads Ctrl+Y there.
useHotkey('Mod+[KeyZ]', () => (physicalFired.value = 'Undo'))
</script>

<template>
  <div class="app">
    <header>
      <h1>Keyboard Layouts</h1>
      <p>
        Type hotkeys with keys from other layouts, match keys by position, and
        label them with the characters on the user's keyboard.
      </p>
    </header>

    <main>
      <section class="demo-section">
        <h2>German keys</h2>
        <p class="note">
          Switch your keyboard layout to German to press these.
        </p>
        <div class="hotkey-list">
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Mod+Ö') }}</kbd> Toggle comment
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Mod+Shift+Ä') }}</kbd> Add annotation
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('ß') }}</kbd> Toggle sidebar
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('§') }}</kbd> Show sections
          </div>
        </div>
        <p role="status">Last fired: {{ germanFired }}</p>
      </section>

      <section class="demo-section">
        <h2>Keys on every layout</h2>
        <div class="hotkey-list">
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Mod+K') }}</kbd> Open command palette
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Mod+Enter') }}</kbd> Send
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Escape') }}</kbd> Dismiss
          </div>
        </div>
        <p role="status">Last fired: {{ everyLayoutFired }}</p>
      </section>

      <section class="demo-section">
        <h2>Physical keys</h2>
        <p class="note">
          These labels show the characters on your keyboard. On French AZERTY,
          the movement keys read Z Q S D.
        </p>
        <div class="hotkey-list">
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('[KeyW]') }}</kbd> Move up
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('[KeyA]') }}</kbd> Move left
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('[KeyS]') }}</kbd> Move down
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('[KeyD]') }}</kbd> Move right
          </div>
          <div class="hotkey-row">
            <kbd>{{ formatForDisplay('Mod+[KeyZ]') }}</kbd> Undo
          </div>
        </div>
        <p role="status">Last fired: {{ physicalFired }}</p>
      </section>

      <section class="demo-section">
        <h2>On-screen keyboard</h2>
        <p class="note">
          {{
            layoutMap
              ? 'The top letter row of your keyboard layout.'
              : 'No layout from the browser, so this is the US top row.'
          }}
        </p>
        <p role="status">
          Top row:
          <template
            v-for="code in [
              'KeyQ',
              'KeyW',
              'KeyE',
              'KeyR',
              'KeyT',
              'KeyY',
              'KeyU',
              'KeyI',
              'KeyO',
              'KeyP',
            ]"
            :key="code"
          >
            <!-- Falls back to the US letter, the code without `Key`. -->
            <kbd>{{
              (layoutMap?.get(code) ?? code.slice(3)).toUpperCase()
            }}</kbd
            >{{ ' ' }}
          </template>
        </p>
      </section>

      <!-- `keyboardLayout: false` stops labels in this provider from asking
           the browser for the layout. Without it, a label with no
           `layoutMap` shows the browser's layout, as above. -->
      <HotkeysProvider :default-options="{ keyboardLayout: false }">
        <OwnLayout />
      </HotkeysProvider>
    </main>

    <TanStackDevtools :plugins="plugins" />
  </div>
</template>