diff --git a/src/index.ts b/src/index.ts index d64927bf..28d97fcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,24 @@ -import hotkeys, {HotkeysEvent} from 'hotkeys-js'; -import {useCallback, useEffect} from "react"; +import hotkeys, { HotkeysEvent } from "hotkeys-js"; +import { useCallback, useEffect } from "react"; type CallbackFn = (event: KeyboardEvent, handler: HotkeysEvent) => void; +type Options = { + filter?: typeof hotkeys.filter; +}; -export function useHotkeys(keys: string, callback: CallbackFn, deps: any[] = []) { +export function useHotkeys( + keys: string, + callback: CallbackFn, + deps: any[] = [], + options: Options = {} +) { const memoisedCallback = useCallback(callback, deps); useEffect(() => { + if (options.filter) hotkeys.filter = options.filter; + hotkeys(keys, memoisedCallback); return () => hotkeys.unbind(keys, memoisedCallback); - }, [memoisedCallback]); + }, [memoisedCallback, options]); }