New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-keyboard-shortcuts

Package Overview
Dependencies
Maintainers
0
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-keyboard-shortcuts

React hook to attach keyboard shortcuts to the document

2.2.3
latest
Source
npm
Version published
Weekly downloads
2.1K
28.44%
Maintainers
0
Weekly downloads
 
Created
Source

use-keyboard-shortcuts

An intuitive React hook to enable keyboard shortcuts.

NPM JavaScript Style Guide

Quick Example

  useKeyboardShortcuts([
    { keys: ["ctrl", "a"], onEvent: event => alert("ctrl + a was pressed") },
    { keys: ["shift", "b"], onEvent: event => alert("shift + b was pressed") },
    { keys: ["Tab"], handleNext },
    { keys: ["Esc"], handleClose },
    { keys: ["Enter"], handleSubmit, preventDefault: false },
  ])

  useKeyboardShortcuts(
    [{ keys: ["ctrl", "shift"], onEvent: () => alert('ctrl + shift + scroll is active') }],
    true,
    [],
    "mousewheel"
  )
}

Features

  • Easy to use
  • Typescript support
  • Multiple shortcuts registered at the same time
  • Support for special keys: Shift, Ctrl (command on Mac), Alt (option on Mac).
  • Support for keydown and mousewheel events
  • Prevents mature propagation. This means that if you have a shortcut for ctrl + a and ctrl + shift + a in the same hook, then the action for ctrl + a will not trigger when pressing ctrl + shift + a.

Install

$ npm install --save use-keyboard-shortcuts
$ yarn add use-keyboard-shortcuts

Usage

import React from "react"
import useKeyboardShortcuts from "use-keyboard-shortcuts"

const Example = () => {
  const [isOpen, setIsOpen] = React.useState(false)

  useKeyboardShortcuts([
    { keys: ["ctrl", "a"], onEvent: event => alert("ctrl + a was pressed") },
    { keys: ["Esc"], onEvent: () => setIsOpen(false) },
  ])

  return <div>...</div>
}

Arguments

Argument (in order)TypeDefaultDescription
shortcutsShortcut[]undefinedArray of Shortcut-objects that should listen to the specified dependencies and eventType. See Shortcut object-section.
activebooleantrueDisables or enables all shortcuts.
dependenciesany[][]List dependencies of the shortcuts
eventType"keydown"/"mousewheel""keydown"Wether it should listen for keyboard or mouse scroll events

Shortcut object

KeyTypeDescription
keysstring[]Combination of keys that needs to be pressed to trigger onEvent().
onEventfunction(event)Action for when combination in keys are pressed.
disabledbooleanUsed to disable a shortcut in particular

Example

See the example-folder for an extended example of how to use this hook with the mousewheel event type.

License

MIT � SAITS

Keywords

react

FAQs

Package last updated on 27 Sep 2024

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