npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hotkeyz

v0.4.1

Published

A tiny dev-friendly keyboard event listener.

Downloads

4

Readme

Hotkeyz

A small (3.2kB min / 1.5kB gzip) dev-friendly keyboard event callback helper.

Installation

npm install hotkeyz

Building the listener

The hotkeyz function expects a config object as parameter and will return a function that you can use as a callback for keyboard events.

The config object lists the different combos you'd like to react to, they should use valid combos as keys and event callbacks as value.

Combos are composed of modifiers and actual keys and should respect the following rules:

  • simple key: {key}
  • modifier: {modifier} - {key}
  • many modifiers: {modifier} + {modifier} [+ ...] - {key}
  • many combos: {combo}, {combo} [, ...]
  • combo sequence (waits up to 1s between each combo): {combo} {combo} [...]

Valid modifiers are:

  • meta
  • ctrl
  • alt
  • shift
import hotkeyz from 'hotkeyz'

const callback = hotkeyz({
  a: () => console.log('Pressed A.'),

  'meta - space': () => console.log('Pressed SPACE while holding META.'),

  'shift + alt + ctrl - esc': () => console.log('SHIFT + ALT + CTRL + ESCAPE'),

  'x, y': e => console.log('Pressed X or Y.', { key: e.key }),

  'a b c': () => console.log('Pressed A, then B, then C.'),

  'meta - k meta - up': e => console.log('Pressed META + K, then META + UP')
})

Using the callback

Hotkeyz only creates a function that acts as a switch for key events, it doesn't do anything DOM related so it'll be your job to add/remove it as an event listener.

Every key event that has a matching hotkey will be captured by the callback: it won't bubble up and its default action will be prevented. For any other key stroke, the browser will behave as it normally would.

Vanilla

const callback = hotkeyz({
  enter: () => console.log('Pressed ENTER')
})

document.addEventListener('keydown', callback)

React

class Hotkeyz extends React.Component {
  state = {
    counter: 0
  }

  increment = () => {
    this.setState({ counter: this.state.counter + 1 })
  }

  handleKey = hotkeyz({
    space: this.increment
  })

  render() {
    return (
      <div tabIndex="0" onKeyDown={this.handleKey}>
        {this.state.counter}
      </div>
    )
  }
}

Key detection

Hotkeys relies on two methods to match your hotkeys with the emitted keyboard event, providing you two ways of describing your hotkeys:

  1. based on event.key: use the actual character you want your listener to react to
  2. based on event.keyCode: use the regular name of the key on your keyboard

keyCode detection is based on keycode so you can name your keys using strings that could be generated by this lib.

hotkeyz({
  a: () => {}, // event.key === 'a'
  'shift - esc': () => {} // event.keyCode === 65 && event.shiftKey === true
})

Note that both methods can react to a same key stroke. If you create 2 hotkeys that do that, both their callbacks will be invoked.

hotkeyz({
  'shift - a': () => {}, // event.keyCode === 65 && event.shiftKey === true
  A: () => {}, // event.key === 'A', invoked along with the callback above

  'shift - /': () => {}, // event.keyCode === 191 && event.shiftKey === true
  '?': () => {} // event.key === '?', invoked along with the callback above
})

If you use the key method, remember that some characters already imply a combination of modifiers. Due to that combination being quite specific to the client, the key method will simply ignore modifiers for them. If you really want to specify a complex combination, you can still rely on the keyCode method.

hotkeyz({
  A: () => {}, // equivalent to `shift - a`
  'shift - A': () => {}, // modifier + shifted event.key => wrong and ignored
  'shift - a': () => {} // modifier + event.keyCode => ok
})

When describing a sequence, you should stick to one of the two methods, hotkeyz won't react to a rule with mixed syntaxes.

hotkeyz({
  'shift - / shift - / shift - /': () => {}, // ok
  '? ? ?': () => {}, // ok
  '? shift - / ?': () => {} // mixed => will never be detected
})

As the characters ,, + and - are reserved by the hotkey syntax, you may not use them directly as keys. If you want to do so, you should use their aliases comma, plus and minus

hotkeyz({
  comma: () => alert('pressed ,'),
  plus: () => alert('pressed +'),
  minus: () => alert('pressed -')
})