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

key-commander

v0.1.2

Published

A centralised keyboard listener for parts of an application to subscribe to

Downloads

116

Readme

key-commander

A centralised keyboard listener for parts of an application to subscribe to.

Runs a single event listener for keyup, keydown, and keypress that can be subscribed to, and the functions you pass in will not be called unless all defined option criterias are satisfied.

You can find a list of accepted special characters here eg: ArrowUp

Install

Using npm

npm i key-commander

Using yarn

yarn add key-commander

API

const kc = {
  subscribe: (key: string, func, options) => subId: string,
  unsub: (subId: string) => void,
  getList: () => Array<{
    id: string,
    key: string,
    func,
    options,
  }>
}

// ---

func: (
  event: KeyboardEvent,
  {
    // Use this to determine if the currently focused element
    // when then event occurs has a tab index such as an input
    // where you may not want to trigger your function
    onTabElement: boolean,
  }
) => void
options: {
  /**
   * At what stage of the user input you would like the function called
   *
   * default: keydown
   */
  event: 'keyup' | 'keydown' | 'keypress'
  /**
   * If you want the function called only when one or more modifiers are active
   * You may experience issues with alt/meta modifiers depending on browsers as
   * they may be attached to other browser functionality.
   *
   * Can also pass `'none'` if you want to specifically trigger only if no modifiers are pressed
   *
   * default: void
   */
  modifier: void | 'alt' | 'ctrl' | 'meta' | 'shift' | Array<'alt' | 'ctrl' | 'meta' | 'shift'>
  /**
   * If you want the function called multiple times if the user holds down a particular key
   *
   * default: false
   */
  onRepeat: boolean
}

Usage

import KeyCommander from 'key-commander';

const kc = new KeyCommander();

const subId = kc.subscribe('escape', () => {});

kc.unsub(subId);

With React:

import React from 'react';
import KeyCommander from 'key-commander';

const kc = new KeyCommander();

const Comp = () => {
  useEffect(() => {
    const id = kc.subscribe('b', (event, { onTabElement }) => {
      if (!onTabElement) {
        // close menu
      }
    }, { onRepeat: true });

    return () => {
      kc.ubsub(id);
    };
  });

  return <div />;
}

Single Instance

Alternatively, if you don't want to manage your own instance you can have key-commander run its own that's stored in an abstracted window object. Otherwise you can manage the instance yourself, by passing the functionality through react context for example.

import kc from 'key-commander/instanced';

You can also simplify this with module resolution with webpack module alias so you only need to type import kc from 'key-commander' when importing.

const webpackConfig = {
  // other options ...
  resolve: {
    alias: {
      'key-commander': 'key-commander/instanced',
    },
    // other resolvers ...
  },
  // other options ...
};

If resolving imports and using flow you may find you're not getting proper typings anymore. To fix this you can update your .flowconfig with module.name_mapper.

[options]
module.name_mapper='^key-commander$' -> 'key-commander/instanced'
# other options ...