Skip to content

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

NameTypeDefaultNote
targetHookTarget | Window-DOM element or ref to attach keyboard listeners to
options.enabled?UseKeysPressedOptionstrueEnable or disable the event listeners

Returns

UseKeysPressedReturn

Parameters

NameTypeDefaultNote
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

SourceDemo

Contributors

D
debabin
debabin
H
hywax
hywax
G
Gorilla Dev
Gorilla Dev

Released under the MIT License.