Skip to content

announceLiveHotkeys

ts
function announceLiveHotkeys(format, getRegistrations): (input, options) => string | undefined;

Defined in: adapter.ts:768

Wraps an ARIA formatter so it returns undefined for a hotkey that can't fire, so screen readers announce only working shortcuts. A hotkey counts when any enabled registration in an active scope announces the same keys, so a disabled control's shortcut is still announced while another registration uses it. Suspended character keys and hotkeys nothing has registered aren't announced. Focus isn't checked, because the attribute sits on the control the shortcut acts on. Read the adapter's registrations reactively in getRegistrations, so the value updates when they change.

Parameters

format

(input, options) => string | undefined

getRegistrations

() => readonly HotkeyRegistrationView[]

Returns

ts
(input, options?): string | undefined;

Formats a hotkey for an aria-keyshortcuts attribute so screen readers can announce the shortcut. Mod+K becomes Meta+K on macOS and Control+K elsewhere.

Modifiers use the ARIA names (Control, Alt, Shift, Meta) and come before the key. Keys use KeyboardEvent.key values, such as K, /, Escape, or ArrowUp, with Space for the space bar and Plus for the plus key. A physical binding names the key at that position, so [KeyQ] becomes Q, or the key from layoutMap such as A on a French AZERTY keyboard. Logical punctuation stays as written, so '?' stays ? even where it takes Shift to type.

Returns undefined when the hotkey has no ARIA form, so the attribute is omitted. This applies to empty or null hotkeys, multi-step sequences, standalone modifiers, and physical codes with no key value such as [NumpadMemoryAdd]. A one-step sequence formats like its chord.

A hotkey object with alternates lists every chord for the platform, separated by spaces, which is how the attribute names more than one shortcut. Its sequences are left out. For two unrelated shortcuts on one control, join both formatted values with a space. In a server-rendered app, use your adapter's formatter or pass platform. Without a platform option, formatForAria uses 'linux' on the server, like formatForDisplay.

Parameters

input

DisplayHotkey | null | undefined

A hotkey string, raw object, parsed chord, hotkey object, or nothing

options?

FormatAriaOptions = {}

The platform and keyboard layout

Returns

string | undefined

The attribute value, or undefined when there is none

Example

ts
formatForAria('Mod+K', { platform: 'mac' }) // 'Meta+K'
formatForAria('Mod+Shift+P', { platform: 'windows' }) // 'Control+Shift+P'
formatForAria('Shift+Space') // 'Shift+Space'
formatForAria(['G', 'H']) // undefined
formatForAria({ default: 'Mod+Y', alternates: ['Mod+Shift+Z'] }, { platform: 'windows' })
// 'Control+Y Control+Shift+Z'