rx-hotkeys
Version:
Advanced Keyboard Shortcut Management library using rxjs
43 lines • 2.1 kB
TypeScript
import { type KeyCombinationConfig, type KeySequenceConfig } from "../../core/index.js";
export type HotkeyHookOptions = Omit<KeyCombinationConfig, "id" | "keys">;
export type SequenceHookOptions = Omit<KeySequenceConfig, "id" | "sequence">;
/**
* React hook to declaratively register a key combination hotkey.
* The hotkey is automatically registered when the component mounts and unregistered when it unmounts.
*
* @param keys The key combination to listen for. Can be a string like "ctrl+s" or an array.
* @param callback The function to execute when the hotkey is triggered.
* @param options An optional configuration object for `preventDefault`, `context`, `target`, etc.
*
* @example
* import { useHotkeys, Keys } from "./wraper";
*
* function MyComponent() {
* const [count, setCount] = useState(0);
*
* // The callback can directly use the latest `count` state without stale closure issues,
* // and we no longer need to pass a dependency array for it.
* useHotkeys("c", () => {
* console.log(`Current count is: ${count}. Incrementing.`);
* setCount(count + 1);
* });
*
* return <div>Count: {count} (Press "c" to increment)</div>;
* }
*/
export declare function useHotkeys(keys: KeyCombinationConfig["keys"], callback: (event: KeyboardEvent) => void, options?: HotkeyHookOptions): void;
/**
* React hook to declaratively register a key sequence hotkey.
* The hotkey is automatically registered when the component mounts and unregistered when it unmounts.
*
* @param sequence The key sequence to listen for. Can be a string like "g -> i" or an array.
* @param callback The function to execute when the hotkey is triggered.
* @param options An optional configuration object for `preventDefault`, `context`, `sequenceTimeoutMs`, etc.
*
* @example
* useSequence("g -> i", () => {
* console.log("Navigating to inbox...");
* }, { sequenceTimeoutMs: 1000 });
*/
export declare function useSequence(sequence: KeySequenceConfig["sequence"], callback: (event: KeyboardEvent) => void, options?: SequenceHookOptions): void;
//# sourceMappingURL=useHotkeys.d.ts.map