@waradu/keyboard
Version:
A keyboard manager compatible with JavaScript, TypeScript and Nuxt
50 lines (49 loc) • 1.2 kB
JavaScript
import { useNuxtApp } from "nuxt/app";
import { getCurrentInstance, onBeforeUnmount, ref } from "vue";
export const useKeybind = ((...args) => {
const { $keyboard } = useNuxtApp();
const vm = getCurrentInstance();
const off = $keyboard.bind(...args);
if (vm) {
onBeforeUnmount(() => {
off();
});
}
return off;
});
export function useKeybindLayer(...args) {
const { $keyboard } = useNuxtApp();
const vm = getCurrentInstance();
const layer = $keyboard.layers.create(...args);
if (vm) {
onBeforeUnmount(() => {
layer.off();
});
}
return layer;
}
export function useKeyboardInspector() {
const { $keyboard } = useNuxtApp();
const allHandlers = ref([]);
const unsubscribe = $keyboard.subscribe((handlers) => {
allHandlers.value = handlers;
});
const vm = getCurrentInstance();
if (vm) {
onBeforeUnmount(() => {
unsubscribe?.();
});
}
return { handlers: allHandlers, unsubscribe };
}
export function useKeybindRecorder(cb) {
const { $keyboard } = useNuxtApp();
const vm = getCurrentInstance();
const off = $keyboard.record(cb);
if (vm) {
onBeforeUnmount(() => {
off();
});
}
return off;
}