UNPKG

@waradu/keyboard

Version:

A keyboard manager compatible with JavaScript, TypeScript and Nuxt

163 lines (160 loc) 4.15 kB
import { K as KeyboardConfig, a as Options, C as Config, S as SubscribeCallback, b as OptionsKeys, c as Keybind } from './shared/keyboard.qu5iZOyZ.mjs'; import './keys.mjs'; declare class Keyboard { private config; private handlers; private allLayers; private disabledLayers; private subscribers; private abortSignalListeners?; ready: boolean; paused: boolean; constructor(config?: KeyboardConfig); private log; private onKeydown; /** * Clear all listeners. */ clear(): void; /** * Removes all event handlers. * To re-enable listening after calling this, call `init()` again. */ stop(): void; /** * Removes all event listeners and registered handlers. * Use this if you are not planning to re-enable listening with `init()` after. */ destroy(): void; /** * Temporarily pause listener */ pause(): void; /** * Resume listener */ resume(): void; /** * Toggle paused state */ toggle(): void; /** * Initialize the keyboard. Call this when `window` is available (it will fail silently). * You can define listeners before initializing. */ init(opts?: { signal?: AbortSignal; }): void; /** * Create new handler. * * @param handler Handler options. * @returns Unlisten function * * @example * ```ts * keyboard.bind({ * keys: "alt+a", * run() { * console.log("Alt + A key pressed"); * } * }); * ``` */ bind(handler: Options): () => void; /** * Create new handlers. * * @param options Multiple handlers with options. * @param config The config that gets applied to all handlers. * @returns Unlisten function * * @example * ```ts * keyboard.bind([ * { * keys: "alt+a", * run() { * console.log("Alt + A key pressed"); * } * }, * { * keys: "alt+b", * run() { * console.log("Alt + B key pressed"); * } * }, * ]); * ``` */ bind(handlers: Options[], config?: Config): () => void; unbind(id: string): void; private notify; /** * Subscribe to changes of all registered keyboard listeners. * * The callback is called: * - once immediately with the current listeners * - on every add/remove/clear of listeners * * @param callback Receives the current list of listeners on each change. * @returns Function to unsubscribe from further updates. */ subscribe(cb: SubscribeCallback): () => void; /** * Check if Key String listener already exists. */ exists(sequence: OptionsKeys): boolean; /** * Record pressed keys and emit them as a Keybind. * * @param callback Receives each recorded keybind. * @returns Function to stop recording. */ record(cb: (keybind: Keybind) => void): () => void; layers: { /** * Create new keybind layer. */ create: (layer: string, disabled?: boolean) => { /** * Disable this layer */ disable: () => void; /** * Enable this layer */ enable: () => void; /** * Enable this layer */ toggle: () => void; bind: typeof this.bind; /** * Disables all listeners in this layer. */ off: () => void; }; /** * Enable specific layers. */ enable: (layers: string | string[]) => void; /** * Disable specific layers. */ disable: (layers: string | string[]) => void; /** * Set active layers. */ set: (layers: string | string[]) => void; /** * Enable all layers. */ all: () => void; /** * Disable all layers. */ none: () => void; }; } export { Keyboard };