isHotkeyPressed API
Function signature:
function isHotkeyPressed(key: string | string[], splitKey: string = ','): boolean
This function allows us to check if a specific key or combination of keys is pressed by the user.
const isHotkeyPressed = useIsHotkeyPressed();
const onClick = () => isHotkeyPressed('shift') ? setCount(count - 1) : setCount(count + 1);
Arguments
key
key: string | string[]
The key or keys to check. This can be a single keycode string (like 'a'), a
delimited string of keycodes (like 'shift,a'), or an array of keycode strings
(like ['shift','a']). If multiple keys are provided, either as a delimited
string or as an array, all keys must be pressed down for the function to return
true.
Special characters that require shift to be held (like !, @, #, ?,
etc.) are also not directly supported; instead, check if the base key is held
(like 1, 2, 3, slash, etc.) along with the shift key (e.g.
['shift','1']).
The mod key is not supported. Please check for ctrl and
meta keys separately.
splitKey
key: string = ','
The delimiter used to parse multiple keys from a single string. This is a comma
(,) by default.
Return value
boolean
The function returns true if the key or key combination is currently pressed
down, otherwise it will return false.