import { onBeforeUnmount, onMounted } from "vue";

export function useHandleWindowKey(key: string, callback: () => void) {

  function handleKeyUp(e: KeyboardEvent) {
    if (e.key === key) {
      e.preventDefault();
      callback();
    }
  }

  onMounted(() => {
    window.addEventListener('keyup', handleKeyUp);
  });
  onBeforeUnmount(() => {
    window.removeEventListener('keyup', handleKeyUp);
  });
}