Files
Agent-n8n/n8n-n8n-1.109.2/packages/frontend/@n8n/composables/src/useShortKeyPress.ts
2025-09-08 04:48:28 +08:00

42 lines
761 B
TypeScript
Executable File

import { onKeyDown, onKeyUp } from '@vueuse/core';
import type { KeyFilter } from '@vueuse/core';
import { ref, unref } from 'vue';
import type { MaybeRefOrGetter } from 'vue';
export function useShortKeyPress(
key: KeyFilter,
fn: () => void,
{
dedupe = true,
threshold = 300,
disabled = false,
}: {
dedupe?: boolean;
threshold?: number;
disabled?: MaybeRefOrGetter<boolean>;
},
) {
const keyDownTime = ref<number | null>(null);
onKeyDown(
key,
() => {
if (unref(disabled)) return;
keyDownTime.value = Date.now();
},
{
dedupe,
},
);
onKeyUp(key, () => {
if (unref(disabled) || !keyDownTime.value) return;
const isShortPress = Date.now() - keyDownTime.value < threshold;
if (isShortPress) {
fn();
}
});
}