UNPKG

react-keyhub

Version:

A lightweight, scalable keyboard shortcut manager for React applications with TypeScript support

125 lines (124 loc) 3.75 kB
import { ShortcutSettings, ShortcutCallback, EventBusOptions, ShortcutContext, ShortcutConfig } from './types'; /** * EventBus class for managing keyboard shortcuts */ export declare class EventBus { private shortcuts; private subscriptions; private shortcutIdToSubscriptions; private options; private isListening; private handleKeyDown; private activeContext; private sequenceBuffer; private sequenceTimer; private paused; private _shortcutIdToKeyCombo; /** * Creates a new EventBus instance * @param shortcuts The shortcut settings * @param options Options for the EventBus */ constructor(shortcuts: ShortcutSettings, options?: EventBusOptions); /** * Starts listening for keyboard events */ private startListening; /** * Stops listening for keyboard events */ stopListening(): void; /** * Pauses the event bus (temporarily disables all shortcuts) */ pause(): void; /** * Resumes the event bus */ resume(): void; /** * Sets the active context * @param context The context to set as active */ setContext(context: ShortcutContext | null): void; /** * Gets the active context * @returns The active context */ getContext(): ShortcutContext | null; /** * Handles sequence shortcuts * @param keyCombo The key combination that was triggered * @param event The original keyboard event */ private handleSequence; /** * Subscribes to a keyboard shortcut * @param shortcutId The ID of the shortcut to subscribe to * @param callback The callback to execute when the shortcut is triggered * @returns A subscription ID that can be used to unsubscribe */ on(shortcutId: string, callback: ShortcutCallback): string; /** * Log all current subscriptions for debugging */ private logSubscriptions; /** * Unsubscribes from a keyboard shortcut * @param subscriptionId The ID of the subscription to remove */ off(subscriptionId: string): void; /** * Emits a keyboard shortcut event * @param keyCombo The key combination that was triggered * @param event The original keyboard event */ private emit; /** * Updates a shortcut configuration * @param shortcutId The ID of the shortcut to update * @param config The new configuration */ updateShortcut(shortcutId: string, config: Partial<ShortcutConfig>): void; /** * Registers a new shortcut * @param shortcutId The ID of the shortcut to register * @param config The shortcut configuration */ registerShortcut(shortcutId: string, config: ShortcutConfig): void; /** * Unregisters a shortcut * @param shortcutId The ID of the shortcut to unregister */ unregisterShortcut(shortcutId: string): void; /** * Enables a shortcut * @param shortcutId The ID of the shortcut to enable */ enableShortcut(shortcutId: string): void; /** * Disables a shortcut * @param shortcutId The ID of the shortcut to disable */ disableShortcut(shortcutId: string): void; /** * Gets all registered shortcuts * @returns The shortcut settings */ getShortcuts(): ShortcutSettings; /** * Gets shortcuts by group * @param group The group to filter by * @returns The shortcuts in the specified group */ getShortcutsByGroup(group: string): ShortcutSettings; /** * Gets all shortcut groups * @returns An array of unique group names */ getShortcutGroups(): string[]; /** * Cleans up the EventBus */ destroy(): void; }