Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/documentation/docs/api/use-hotkeys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const options = {
document: undefined,
ignoreModifiers: false,
useKey: false,
includeModifiersInKeys: false,
};
```

Expand Down Expand Up @@ -381,6 +382,17 @@ useKey: boolean // default: false
This option allows us to listen to the produced key instead of the key code. This is useful if we want to listen to a
specific key (e.g. `!`, `:`, `%`, etc.) and don't care about how the user produces that key.


##### `includeModifiersInKeys`

```ts
includeModifiersInKeys: boolean // default: false
```

When set to `true`, the `keys` property of the `handler` object will include the modifier keys (e.g. `shift`, `ctrl`, etc.)
This is useful if we want to know which modifier keys were pressed in addition to the main key. For example, if the user
presses `ctrl+s`, the `keys` property will contain `['ctrl', 's']` instead of just `['s']`.

***

### `deps`
Expand Down
5 changes: 3 additions & 2 deletions packages/react-hotkeys-hook/src/lib/parseHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function parseHotkey(
sequenceSplitKey = '>',
useKey = false,
description?: string,
includeModifiersInKeys = false,
): Hotkey {
let keys: string[] = []
let isSequence = false
Expand All @@ -65,11 +66,11 @@ export function parseHotkey(
useKey,
}

const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))
const filteredKeys = includeModifiersInKeys ? keys : keys.filter((k) => !reservedModifierKeywords.includes(k))

return {
...modifiers,
keys: singleCharKeys,
keys: filteredKeys,
description,
isSequence,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-hotkeys-hook/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export type Options = {
sequenceTimeoutMs?: number
// The character to split the sequence of keys. (Default: >)
sequenceSplitKey?: string
// Include modifier keys in `keys` array (Default: false)
includeModifiersInKeys?: boolean
}

export type OptionsOrDependencyArray = Options | DependencyList
3 changes: 3 additions & 0 deletions packages/react-hotkeys-hook/src/lib/useHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default function useHotkeys<T extends HTMLElement>(
memoisedOptions?.sequenceSplitKey,
memoisedOptions?.useKey,
memoisedOptions?.description,
memoisedOptions?.includeModifiersInKeys,
)

if (hotkey.isSequence) {
Expand Down Expand Up @@ -215,6 +216,7 @@ export default function useHotkeys<T extends HTMLElement>(
memoisedOptions?.sequenceSplitKey,
memoisedOptions?.useKey,
memoisedOptions?.description,
memoisedOptions?.includeModifiersInKeys,
),
),
)
Expand All @@ -235,6 +237,7 @@ export default function useHotkeys<T extends HTMLElement>(
memoisedOptions?.sequenceSplitKey,
memoisedOptions?.useKey,
memoisedOptions?.description,
memoisedOptions?.includeModifiersInKeys,
),
),
)
Expand Down
15 changes: 14 additions & 1 deletion packages/react-hotkeys-hook/src/test/useHotkeys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1650,4 +1650,17 @@ test('Should trigger only produced key hotkeys', async () => {
await user.keyboard('Y')
expect(callbackZ).toHaveBeenCalledTimes(1)
expect(callbackY).toHaveBeenCalledTimes(2)
})
})

test('Should allow passing through modifier keys in keys array', async () => {
const user = userEvent.setup()

const callbackZ = vi.fn()

renderHook(() => useHotkeys(['ctrl+alt+z'], (_, {keys}) => {
callbackZ(keys)
}, {useKey: true, includeModifiersInKeys: true}))

await user.keyboard('{Control>}{Alt>}z{/Alt}{/Control}')
expect(callbackZ).toHaveBeenCalledWith(['ctrl', 'alt', 'z'])
})