KeyboardShortcutsManager
KeyboardShortcutsManager installs keyboard shortcuts on an event source that
forwards DOM keydown events.
Usage
Use KeyboardShortcutsManager when your application already has an event
manager that forwards keyboard events.
import {
KeyboardShortcutsManager,
type KeyboardShortcut
} from '@deck.gl-community/panels';
const shortcuts: KeyboardShortcut[] = [
{
key: '/',
commandKey: true,
name: 'Show shortcuts',
description: 'Open keyboard shortcuts',
preventDefault: true,
onKeyPress: () => setOpen(true)
}
];
const manager = new KeyboardShortcutsManager(eventManager, shortcuts);
manager.start();
Use KeyboardShortcutsManagerDocument when shortcuts should be installed
directly on document.
import {
KeyboardShortcutsManagerDocument,
type KeyboardShortcut
} from '@deck.gl-community/panels';
const shortcuts: KeyboardShortcut[] = [
{
key: 'Escape',
name: 'Close',
description: 'Close the active panel',
onKeyPress: () => setOpen(false)
}
];
const manager = new KeyboardShortcutsManagerDocument(shortcuts);
manager.start();
Types
type KeyboardShortcutManagerEvent = {
srcEvent: KeyboardEvent;
};
type KeyboardShortcutEventManager = {
on(event: 'keydown', handler: (event: KeyboardShortcutManagerEvent) => void): void;
off(event: 'keydown', handler: (event: KeyboardShortcutManagerEvent) => void): void;
};
type KeyboardShortcutDisplayPair = {
id: string;
position: 'primary' | 'secondary';
description: string;
};
type KeyboardShortcut = {
key: string;
name: string;
description: string;
onKeyPress?: () => void;
commandKey?: boolean;
ctrlKey?: boolean;
shiftKey?: boolean;
dragMouse?: boolean;
badges?: string[];
preventDefault?: boolean;
displayInputs?: ShortcutDisplayInput[];
displaySection?: 'navigation' | 'interaction' | 'commands' | 'settings';
displayPair?: KeyboardShortcutDisplayPair;
};
Methods
start(): void;
stop(): void;
Remarks
KeyboardShortcutsManagerworks with any event manager that implements theon('keydown', ...)andoff('keydown', ...)contract.KeyboardShortcutsManagerDocumentis the simplest option for stand-alone DOM usage.- Both managers dispatch
onKeyPresson the most specific matchingKeyboardShortcut. - Shortcut matching is exact for modifiers: extra command, control, shift, or alt modifiers do not trigger shortcuts unless the shortcut explicitly matches them. Alt-key shortcuts are reserved for future support.
- Set
preventDefaulton a shortcut to prevent the browser default behavior after it matches. - Shortcut matching is shared with
KeyboardShortcutsPaneland the exported keyboard shortcut helpers.