useKeysPressed
all currently pressed keyboard keys and their codes
sensors
low
test coverage
Last changed: last month
Installation
Library
CLI
Manual
typescript
import { useKeysPressed } from '@siberiacancode/reactuse';Usage
typescript
const { value } = useKeysPressed(ref);
// or
const { value, ref } = useKeysPressed();Demo
Api
Parameters
| Name | Type | Default | Note |
|---|---|---|---|
| target | HookTarget | Window | - | DOM element or ref to attach keyboard listeners to |
| options.enabled? | UseKeysPressedOptions | true | Enable or disable the event listeners |
Returns
UseKeysPressedReturn
Parameters
| Name | Type | Default | Note |
|---|---|---|---|
| options? | UseKeysPressedOptions | - | - Optional configuration options |
Returns
UseKeysPressedReturn & { ref: StateRef<Target> }
Type declaration
typescript
import type { HookTarget } from '@/utils/helpers';
import type { StateRef } from '../useRefState/useRefState';
export interface UseKeysPressedOptions {
/** Enable or disable the event listeners */
enabled?: boolean;
}
export interface UseKeysPressedReturn {
/** The array of currently pressed keys */
value: Array<{ key: string; code: string }>;
}
export interface UseKeysPressed {
(target: HookTarget | Window, options?: UseKeysPressedOptions): UseKeysPressedReturn;
<Target extends Element>(
options?: UseKeysPressedOptions
): UseKeysPressedReturn & { ref: StateRef<Target> };
}Source
Source • DemoContributors
D
H
G