Skip to content

Commit 113480e

Browse files
committed
Add ignoreRepeat option to skip callback on held keys
Uses KeyboardEvent.repeat to detect when a key is being held down and skips the callback invocation when ignoreRepeat is enabled.
1 parent f2006c7 commit 113480e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

packages/react-hotkeys-hook/src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export type Options = {
8787
sequenceTimeoutMs?: number
8888
// The character to split the sequence of keys. (Default: >)
8989
sequenceSplitKey?: string
90+
// Ignore repeated events when the key is held down. (Default: false)
91+
ignoreRepeat?: boolean
9092
// MetaData | Custom data to store and retrieve with the hotkey (Default: undefined)
9193
metadata?: Record<string, unknown>
9294
}

packages/react-hotkeys-hook/src/lib/useHotkeys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export default function useHotkeys<T extends HTMLElement>(
6767
let sequenceTimer: NodeJS.Timeout | undefined
6868

6969
const listener = (e: KeyboardEvent, isKeyUp = false) => {
70+
if (memoisedOptions?.ignoreRepeat && e.repeat) {
71+
return
72+
}
73+
7074
if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, memoisedOptions?.enableOnFormTags)) {
7175
return
7276
}

0 commit comments

Comments
 (0)