useKeyPressEvent ​
Hook that listens for key press events on specified targets
sensors
test coverage
Last changed: last month
Installation ​
Library
CLI
Manual
typescript
import { useKeyPressEvent } from '@siberiacancode/reactuse';
Usage ​
typescript
useKeyPressEvent(ref, 'Enter', () => console.log('pressed'));
// or
const ref = useKeyPressEvent('Enter', (event) => console.log('pressed'));
Demo ​
Api ​
Parameters
Name | Type | Default | Note |
---|---|---|---|
key | UseKeyPressEventKey | - | The key or array of keys to listen for. |
target | HookTarget | Window | - | The target to attach the event listener to. |
listener | (event: KeyboardEvent) => void | - | The callback function to be executed when the specified key or keys are pressed. |
options? | UseKeyPressEventOptions | - | The options for the event listener. |
Returns
void
Parameters
Name | Type | Default | Note |
---|---|---|---|
key | UseKeyPressEventKey | - | The key or array of keys to listen for. |
listener | (event: KeyboardEvent) => void | - | The callback function to be executed when the specified key or keys are pressed. |
options? | UseKeyPressEventOptions | - | The options for the event listener. |
Returns
{ ref: StateRef<Target> }
Type declaration ​
typescript
import type { HookTarget } from '@/utils/helpers';
import type { StateRef } from '../useRefState/useRefState';
export type UseKeyPressEventKey = string | string[];
export interface UseKeyPressEventOptions {
/** Whether the event should be captured */
capture?: boolean;
/** Whether the event listener should only be triggered once */
once?: boolean;
/** Whether the event listener should be passive */
passive?: boolean;
}
export interface UseKeyPressEvent {
(
target: HookTarget | Window,
key: UseKeyPressEventKey,
listener: (event: KeyboardEvent) => void,
options?: UseKeyPressEventOptions
): void;
<Target extends Element>(
key: UseKeyPressEventKey,
listener: (event: KeyboardEvent) => void,
options?: UseKeyPressEventOptions,
target?: never
): { ref: StateRef<Target> };
}
Source ​
Source • DemoContributors ​
D
B
W