import { useEffect } from "react";
import { log } from "../util/logger";

export function useKeyboardShortcut() {
  useEffect(() => {
    function handleKeyDown(event: KeyboardEvent) {
      if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) {
        log('keyboard event detected, but parent window is not focused. returning focus to parent');
        window.blur();
        if (window.parent && window.parent !== window) {
          window.parent.focus();
        }
      }
    }

    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
    };
  }, []);
}
