UNPKG

@stacksjs/stx

Version:

A performant UI Framework. Powered by Bun.

100 lines 2.59 kB
/** * Track keyboard state */ export declare function useKeyboard(options?: KeyboardOptions): KeyboardRef; /** * Register a hotkey handler */ export declare function useHotkey(hotkey: string | string[], callback: HotkeyCallback, options?: HotkeyOptions): () => void; /** * Check if a specific key is currently pressed */ export declare function useKeyPressed(key: string, options?: KeyboardOptions): { get: () => boolean subscribe: (fn: (pressed: boolean) => void) => () => void }; /** * Detect key sequences (like Konami code) */ export declare function useKeySequence(sequence: string[], callback: () => void, options?: { timeout?: number; target?: Window | HTMLElement | Document | null }): () => void; /** * Common keyboard shortcuts helper */ export declare const shortcuts: { save: 'ctrl+s'; undo: 'ctrl+z'; redo: 'ctrl+shift+z'; copy: 'ctrl+c'; paste: 'ctrl+v'; cut: 'ctrl+x'; selectAll: 'ctrl+a'; find: 'ctrl+f'; replace: 'ctrl+h'; newTab: 'ctrl+t'; closeTab: 'ctrl+w'; refresh: 'ctrl+r'; escape: 'escape'; enter: 'enter'; // Mac alternatives saveMac: 'meta+s'; undoMac: 'meta+z'; redoMac: 'meta+shift+z'; copyMac: 'meta+c'; pasteMac: 'meta+v'; cutMac: 'meta+x' }; /** * useKeyboard - Reactive keyboard input tracking * * Track keyboard state, handle hotkeys, and manage keyboard shortcuts. * * @example * ```ts * // Track all keyboard state * const keyboard = useKeyboard() * keyboard.subscribe(state => { * if (state.ctrl && state.pressed.has('s')) { * console.log('Ctrl+S pressed!') * } * }) * * // Simple hotkey * useHotkey('ctrl+s', (e) => { * e.preventDefault() * save() * }) * * // Check if specific key is pressed * const isSpacePressed = useKeyPressed('Space') * ``` */ export declare interface KeyboardState { pressed: Set<string> lastKey: string | null lastEvent: KeyboardEvent | null ctrl: boolean alt: boolean shift: boolean meta: boolean timestamp: number } export declare interface KeyboardOptions { target?: Window | HTMLElement | Document | null capture?: boolean passive?: boolean } export declare interface KeyboardRef { get: () => KeyboardState subscribe: (fn: (state: KeyboardState) => void) => () => void isPressed: (key: string) => boolean stop: () => void } export declare interface HotkeyOptions { target?: Window | HTMLElement | Document | null preventDefault?: boolean stopPropagation?: boolean ignoreRepeat?: boolean keyup?: boolean ignoreInputs?: boolean } declare type HotkeyCallback = (event: KeyboardEvent) => void