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

keyboardist

v2.0.2

Published

simple global keyboard manager

Downloads

492

Readme

🎹 Keyboardist: Declarative keyboard listener

A declarative way to add keyboard shortcuts to your browser applications. Here is a simple demo

For using with React, there's React Keyboardist.

Example:

import { createListener } from "keyboardist";

// by default it listens to keydown
const listener = createListener();

listener.subscribe("Down", () => {
  console.log("Pressed down");
});

listener.subscribe("Shift+Down", () => {
  console.log("Pressed Shift + down");
});

Install

Example:

npm install keyboardist

Usage

The Keyboardist constructor returns a listener object that has only one method: subscribe.

subscribe accepts two arguments: a key or key combination and a method that will be called when that key (or key combination) is triggered by the user.

Example:

import { createListener } from "keyboardist";

const listener = new createListener();

const keySubscription = listener.subscribe("Slash", () => {
  focusSearch();
});

The object returned by subscribe has an unsubscribe method.

// create a subscription
const keySubscription = listener.subscribe("Slash", () => {
  focusSearch();
});

// remove the subscription
keySubscription.unsubscribe();

Multiple listeners for a Key

You can add multiple listeners for the same key, and they will be executed starting from the last one.

// create a subscription

listener.subscribe("Space", () => {
  console.log("A");
});

listener.subscribe("Space", () => {
  console.log("B");
});

listener.subscribe("Space", () => {
  console.log("C");
});

// the console will log 'C', then 'B', then 'A' when the spacebr is pressed.

Key Monitor

Keyboardist's event listener has a setMonitor method that let's you set a function that will monitor all key events. You can either pass true to use the default built-in monitor or pass a function.

Default monitor is useful in development when you don't know the correct key name you want to use.

const listener = new Keyboardist();
// ue the default monitor
listener.setMonitor(true);

// will show the key names / combination as you type them. For example:
// `:keyboard event: KeyA`
// `:keyboard event: Slash`
// `:keyboard event: Shift+Space`

You can also pass a custom function that accepts three parameters: keyName, matched which is true if there's at least a listener for that key, and the originalEvent (which has already been preventDefaulted at this point).

const listener = new Keyboardist();
// ue the default monitor
listener.setMonitor((keyName, matched, originalEvent) => {
  document.getElementById("monitor").innerHTML = `You pressed ${keyName}`;
});

Other events

By default, the listener listens to keydown events, but you can pass keyup as an argument to Keyboardist to use that event instead

const downListener = createListener();
const upListener = Keyboardist("keyup");

downListener.subscribe("a", () => {
  console.log("Just pressed the A key");
});

upListener.subscribe("a", () => {
  console.log("Just released the A key");
});

Stop listening

Internally createListener attaches only one event listener to your document to listen to your keystrokes. If you ever want to remove that listener you can call stopListening on the listener instance.

const listener = createListener();

listener.subscribe("a", () => {
  console.log("Just pressed the A key");
});

// Remove the event listener from the document
listener.stopListening();

// Reattach it again:
listener.startListening();