mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-28 16:49:36 +07:00
21 lines
515 B
TypeScript
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;
|