Skip to content

Commit

Permalink
useKeyDown: add ability to listen for any key with '*'
Browse files Browse the repository at this point in the history
  • Loading branch information
warpdesign committed Apr 16, 2024
1 parent 280a2b4 commit 8cfcb3d
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions src/hooks/useKeyDown.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
Expand Down

0 comments on commit 8cfcb3d

Please sign in to comment.