import {onMounted, onUnmounted} from "vue";


export function useWindowEvent<K extends keyof WindowEventMap>(event: K,  listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined){
    onMounted(() => {
        window.addEventListener(event, listener, options as any)
    })

    onUnmounted(() => {
        window.removeEventListener(event, listener)
    })
}

export function onKeyPress(key: string, listener: () => void) {
    useWindowEvent("keydown", (event) => {
        if(event.key == key) {
            listener()
        }
    })
}