import { Component, computed, signal } from '@angular/core'
import {
injectFormatForDisplay,
injectHotkey,
injectKeyboardLayout,
provideHotkeys,
} from '@tanstack/angular-hotkeys'
import type { GermanKeys, UsKeys } from '@tanstack/angular-hotkeys'
declare module '@tanstack/angular-hotkeys' {
interface Register {
keys: UsKeys | GermanKeys
}
}
@Component({
selector: 'app-own-layout',
standalone: true,
providers: [provideHotkeys({ defaultOptions: { keyboardLayout: false } })],
template: `
<section class="demo-section">
<h2>Supply your own layout</h2>
<p class="note">
When your app stores the user's layout, pass it as
<code>layoutMap</code>. These are the physical keys from above. This
section's provider sets <code>keyboardLayout: false</code>, so US QWERTY
shows US keys whatever your keyboard is.
</p>
<label>
Keyboard layout
<select #select [value]="layout()" (change)="layout.set(select.value)">
<option value="us">US QWERTY</option>
<option value="fr">French AZERTY</option>
<option value="de">German QWERTZ</option>
</select>
</label>
@let map = layoutMap();
<p role="status">
Move: {{ formatForDisplay('[KeyW]', { layoutMap: map }) }}
{{ formatForDisplay('[KeyA]', { layoutMap: map }) }}
{{ formatForDisplay('[KeyS]', { layoutMap: map }) }}
{{ formatForDisplay('[KeyD]', { layoutMap: map }) }}. Undo:
{{ formatForDisplay('Mod+[KeyZ]', { layoutMap: map }) }}
</p>
</section>
`,
})
export class OwnLayout {
readonly formatForDisplay = injectFormatForDisplay()
readonly layout = signal('us')
readonly layoutMap = computed(() =>
this.layout() === 'fr'
? new Map([
['KeyQ', 'a'],
['KeyA', 'q'],
['KeyW', 'z'],
['KeyZ', 'w'],
])
: this.layout() === 'de'
? new Map([
['KeyY', 'z'],
['KeyZ', 'y'],
])
: undefined,
)
}
@Component({
selector: 'app-root',
standalone: true,
imports: [OwnLayout],
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:
@for (
code of [
'KeyQ',
'KeyW',
'KeyE',
'KeyR',
'KeyT',
'KeyY',
'KeyU',
'KeyI',
'KeyO',
'KeyP',
];
track code
) {
<!-- Falls back to the US letter, the code without "Key". Angular
drops whitespace between elements, so the space is a
string. -->
<kbd>{{
(layoutMap()?.get(code) ?? code.slice(3)).toUpperCase()
}}</kbd
>{{ ' ' }}
}
</p>
</section>
<app-own-layout />
</main>
</div>
`,
})
export class AppComponent {
readonly formatForDisplay = injectFormatForDisplay()
readonly layoutMap = injectKeyboardLayout()
readonly germanFired = signal('none')
readonly everyLayoutFired = signal('none')
readonly physicalFired = signal('none')
constructor() {
injectHotkey('Mod+Ö', () => this.germanFired.set('Toggle comment'))
injectHotkey('Mod+Shift+Ä', () => this.germanFired.set('Add annotation'))
injectHotkey('ß', () => this.germanFired.set('Toggle sidebar'))
injectHotkey('§', () => this.germanFired.set('Show sections'))
injectHotkey('Mod+K', () =>
this.everyLayoutFired.set('Open command palette'),
)
injectHotkey('Mod+Enter', () => this.everyLayoutFired.set('Send'))
injectHotkey('Escape', () => this.everyLayoutFired.set('Dismiss'))
injectHotkey('[KeyW]', () => this.physicalFired.set('Move up'))
injectHotkey('[KeyA]', () => this.physicalFired.set('Move left'))
injectHotkey('[KeyS]', () => this.physicalFired.set('Move down'))
injectHotkey('[KeyD]', () => this.physicalFired.set('Move right'))
injectHotkey('Mod+[KeyZ]', () => this.physicalFired.set('Undo'))
}
}