1.0.5 • Published 3 years ago

@react-typed-hooks/use-key-press v1.0.5

Weekly downloads
207
License
MIT
Repository
github
Last release
3 years ago

useKeyPress

React hook that returns true when the specified targetKey is pressed.

version

Installation

npm install @react-typed-hooks/use-key-press

Usage

import { useKeyPress } from "@react-typed-hooks/use-key-press";

const Demo = () => {
  const isEscapePressed = useKeyPress({ targetKey: "Escape" });

  return isEscapePressed ? "Yes" : "No";
};

API

Types

interface UseKeyPressOptions {
  targetKey: KeyboardEvent["key"];
}
function useKeyPress(options: UseKeyPressOptions): boolean;

Options

targetKey: KeyboardEvent["key"]

A predefined key value (i.e, Escape, Enter, Backspace, etc) to watch the keydown and keyup event on.