Skip to content

loadKeyboardLayout

ts
function loadKeyboardLayout(): () => void;

Defined in: keyboard-layout.ts:96

Loads the browser's keyboard layout into getKeyboardLayoutStore(), with navigator.keyboard.getLayoutMap(). Only Chromium browsers have it, so the layout stays null elsewhere and labels keep their conventional form.

Framework adapters call this for you when a label or layout primitive mounts. Call it yourself only without an adapter, in the browser, after your page has rendered.

The first caller reads the layout. Browsers send no event when the user switches layouts, so while any caller is active, the layout is read again each time the window regains focus.

Returns

A function that ends this caller's interest. When the last caller stops, refreshing stops. The loaded layout stays in the store, so the next label has it at once.

() => void

Example

ts
const stop = loadKeyboardLayout()
getKeyboardLayoutStore().subscribe((layoutMap) => {
  label.textContent = formatForDisplay('[KeyW]', {
    layoutMap: layoutMap ?? undefined,
  })
})