feat: implement useKeyDown composable
This commit is contained in:
15
app/composables/useKeyDown.ts
Normal file
15
app/composables/useKeyDown.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export type KeyDownCallback = (key: string) => void;
|
||||
|
||||
export const useKeyDown = (callback: KeyDownCallback) => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
callback(event.key);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener("keydown", handleKeyDown);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user