From 8cfcb3d575760cc76264430077b82a5184b0f654 Mon Sep 17 00:00:00 2001 From: Nicolas Ramz Date: Tue, 16 Apr 2024 10:10:59 +0200 Subject: [PATCH] useKeyDown: add ability to listen for any key with '*' --- src/hooks/useKeyDown.ts | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/hooks/useKeyDown.ts b/src/hooks/useKeyDown.ts index 3c8e52cb..70bf298d 100644 --- a/src/hooks/useKeyDown.ts +++ b/src/hooks/useKeyDown.ts @@ -1,30 +1,9 @@ import { shouldCatchEvent } from '$src/utils/dom' import React from 'react' -// export const useKeyDown = (callback: (ev: KeyboardEvent) => void, keys: string[]) => { -// const onKeyDown = React.useCallback( -// (event: KeyboardEvent) => { -// const wasAnyKeyPressed = keys.some((key) => event.key === key) -// if (wasAnyKeyPressed && shouldCatchEvent(event)) { -// callback(event) -// } -// }, -// [callback, keys], -// ) - -// React.useEffect(() => { -// document.addEventListener('keydown', onKeyDown) -// return () => { -// document.removeEventListener('keydown', onKeyDown) -// debugger -// console.log('removing event listener') -// } -// }, [onKeyDown]) -// } - export function useKeyDown(callback: (ev: KeyboardEvent) => void, keys: string[]) { function handleKeyDown(event: KeyboardEvent) { - if (keys.includes(event.key) && shouldCatchEvent(event)) { + if ((keys.includes(event.key) || keys.includes('*')) && shouldCatchEvent(event)) { callback(event) } }