# @accessible/use-key

> A React hook for handling `keydown` events on elements

Latest version **1.0.2** (published 2020-07-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @accessible/use-key
pnpm add @accessible/use-key
yarn add @accessible/use-key
bun add @accessible/use-key
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-07-08 |
| First published | 2020-07-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 33.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react hook, use key, usekey, keydown hook, use keydown, usekeydown |

## Links

- npm: https://www.npmjs.com/package/@accessible/use-key
- Repository: https://github.com/accessible-ui/use-keycode
- Homepage: https://github.com/accessible-ui/use-keycode#readme
- Issues: https://github.com/accessible-ui/use-keycode/issues
- npm.io page: https://npm.io/package/@accessible/use-key

## Dependencies (1)

- [@react-hook/event](https://npm.io/package/@react-hook/event.md) ^1.2.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-07-08
- 1.0.1 — 2020-07-08
- 1.0.0 — 2020-07-07

## README

<hr>
<div align="center">
  <h1 align="center">
    useKey()
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@accessible/use-key">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@accessible/use-key?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Code coverage report" href="https://codecov.io/gh/accessible-ui/use-key">
    <img alt="Code coverage" src="https://img.shields.io/codecov/c/gh/accessible-ui/use-key?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.org/accessible-ui/use-key">
    <img alt="Build status" src="https://img.shields.io/travis/accessible-ui/use-key?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@accessible/use-key">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@accessible/use-key?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@accessible/use-key?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @accessible/use-key</pre>
<hr>

A React hook for handling `keydown` events on specific `event.key` values. It also
normalizes non-standard `event.key` values from IE to their modern equivalents.

## Quick Start

```jsx harmony
import * as React from 'react'
import useKey from '@accessible/use-key'

const Component = () => {
  const ref = React.useRef(null)

  // Listens to keydown events on the `ref` above
  useKey(ref, {
    // Logs event when the Escape key is pressed
    Escape: console.log,
    // Logs "Key was pressed: Enter" to the console when Enter is pressed
    Enter: (event) => console.log('Key was pressed:', event.key),
  })

  // Listens to keydown events on the window
  useKey(window, {
    // Logs event when the Escape key is pressed
    Escape: console.log,
    // Logs "Key was pressed: Enter" to the console when Enter is pressed
    Enter: (event) => console.log('Key was pressed:', event.key),
  })

  return <div ref={ref} />
}
```

## API

### useKey(target, handlers)

#### Arguments

| Argument | Type                                                                                     | Required? | Description                                                                                                                    |
| -------- | ---------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------ |
| target   | <code>React.RefObject&lt;T&gt; &#124; T &#124; Window &#124; Document &#124; null</code> | Yes       | A React ref, element, `window`, or `document` to add the key listener to                                                       |
| handlers | `Record<string, (event?: KeyboardEvent) => any>`                                         | Yes       | A mapping with keys matching the `event.key` string you want to listen on. The value for each key should be an event listener. |

#### Returns `void`

## LICENSE

MIT

---
_Source: https://npm.io/package/@accessible/use-key · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
