code-share/hooks/useCommandKey.ts

21 lines
515 B
TypeScript

import { useEffect } from "react";
const useCommandKey = (key: string, callback: () => void) => {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.key === key) {
e.preventDefault();
e.stopPropagation();
callback();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [key, callback]);
};
export default useCommandKey;