Skip to content

Commit 0219c01

Browse files
Merge pull request #1311 from ShrimpCryptid/feat/clarify-ishotkeypressed-multiple-key-behavior
fix: Fixed incorrect documentation for `isHotkeyPressed` arguments
2 parents ae74e90 + 9203b7f commit 0219c01

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

packages/documentation/docs/api/is-hotkey-pressed.mdx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Function signature:
1111
function isHotkeyPressed(key: string | string[], splitKey: string = ','): boolean
1212
```
1313

14-
This function allows us to check if a specific key is pressed by the user.
14+
This function allows us to check if a specific key or combination of keys is pressed by the user.
1515

1616
```jsx
1717
const isHotkeyPressed = useIsHotkeyPressed();
@@ -29,19 +29,34 @@ const onClick = () => isHotkeyPressed('shift') ? setCount(count - 1) : setCount(
2929
key: string | string[]
3030
```
3131

32-
The key we want to listen to. This can be a string (like `'a'`, `'shift+a'`) or an array of strings (like `['a', 'shift+a']`).
32+
The key or keys to check. This can be a single keycode string (like `'a'`), a
33+
delimited string of keycodes (like `'shift,a'`), or an array of keycode strings
34+
(like `['shift','a']`). If multiple keys are provided, either as a delimited
35+
string or as an array, all keys must be pressed down for the function to return
36+
`true`.
37+
38+
Special characters that require `shift` to be held (like `!`, `@`, `#`, `?`,
39+
etc.) are also not directly supported; instead, check if the base key is held
40+
(like `1`, `2`, `3`, `slash`, etc.) along with the `shift` key (e.g.
41+
`['shift','1']`).
42+
43+
:::caution Warning
44+
The `mod` key is not supported. Please check for `ctrl` and
45+
`meta` keys separately.
46+
:::
3347

3448
### splitKey
3549

3650
```ts
3751
key: string = ','
3852
```
3953

40-
We can combine to check for multiple keys in one string by separating them with a comma (`,`). We can change
41-
this by passing a different key.
54+
The delimiter used to parse multiple keys from a single string. This is a comma
55+
(`,`) by default.
4256

4357
## Return value
4458

4559
### boolean
4660

47-
The function returns `true` if the key or keystroke is currently pressed down, otherwise it will return `false`.
61+
The function returns `true` if the key or key combination is currently pressed
62+
down, otherwise it will return `false`.

packages/documentation/docs/documentation/is-hotkey-pressed.mdx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar_position: 4
44

55
# isHotkeyPressed
66

7-
This function allows us to check if the user is currently pressing down a key.
7+
This function allows us to check if the user is currently pressing down a key or a combination of keys.
88

99
## Basic Usage
1010

@@ -43,4 +43,22 @@ function ExampleComponent() {
4343
}
4444
```
4545

46-
Just like with `useHotkeys`, you can pass an array as the first argument to check for multiple keys.
46+
You can pass an array or delimited string as the first argument to check if
47+
multiple keys are pressed simultaneously. Commas (`,`) are used as the default
48+
delimiter.
49+
50+
```jsx live
51+
function ExampleComponent() {
52+
const [pressed, setPressed] = useState(false);
53+
54+
// You can also pass a string with delimiters like 'shift,a'
55+
const onClick = () => setPressed(isHotkeyPressed(['shift', 'a']));
56+
57+
return (
58+
<div>
59+
<p>The hotkey was: {pressed ? 'pressed' : 'not pressed'}</p>
60+
<button onClick={onClick}>Check if hotkey is pressed</button>
61+
</div>
62+
)
63+
}
64+
```

0 commit comments

Comments
 (0)