Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@accessible/use-key

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accessible/use-key

A React hook for handling `keydown` events on elements

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
894
decreased by-27.96%
Maintainers
1
Weekly downloads
 
Created
Source

useKey()

Bundlephobia Code coverage Build status NPM Version MIT License

npm i @accessible/use-key

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

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
ArgumentTypeRequired?Description
targetReact.RefObject<T> | T | Window | Document | nullYesA React ref, element, window, or document to add the key listener to
handlersRecord<string, (event?: KeyboardEvent) => any>YesA 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

Keywords

FAQs

Package last updated on 08 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc