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

when-key-events

v1.8.4

Published

Powerful keyboard shortcut and focus library

Downloads

6

Readme

When

Documentation

Installation

Features

Examples

Installation

Install from NPM

npm i --save when-key-events

Wherever you want to use it:

import When from 'when-key-events';

or use the CDN:

<script src="https://unpkg.com/when-key-events@latest/dist/When.js"></script>

A powerful, human readable keyboard shortcut/binding library and focus system

When aims to make complex keyboard shortcuts arbitrary and quick to set up, understand and change, while enabling features you don't get out of the box with DOM events. The goal is to give you granular control over when/how you react to your user's keyboard inputs, while keeping the code human readable and easy to maintain.

A "focus" system is included in When which facilitates the same concept of "focus" found in operating systems, so shortcuts can be specific to certain elements on your page, and you can tie into this system to make the user's focus very apparent to them in any way you see fit.

Features

  • easily set up complicated keyboard shortcuts including:
    • single or multi key shortcuts with modifier and meta key support
    • character sequence shortcuts
    • access to pressed, released and held event types
    • limit the time a shortcut must be performed within from start to end (or set it as long as you like!)
    • default behaviour prevention
    • one time shortcuts
    • chain together all these options in any way you like
  • "focus" system for limiting when/how shortcuts get triggered depending on where the user last clicked
  • "modes" which allow you to easily group shortcuts together and only have one set of shorcuts enabled at a time
  • "groups" which allow you to pause/unpause/toggle/remove/trigger multiple shortcuts at a time
  • register "commands" for re-usable event handlers addressable by strings
    • tie your features to a command name instead of using a literal function, making swapping commands out easy!
  • automatically generate keyboard shortcut documentation for users and developers
  • shortcuts can be programatically executed without input from the user
  • shortcuts can be temporarily paused and unpaused, or completely removed programmatically
  • clear errors that explain how When can/should be used

Examples

Setting up a named event handler

When.command('EXAMPLE_COMMAND', () => {
  console.log('Thanks for using When!')
});

A simple, single key shortcut

When('a').Execute('EXAMPLE_COMMAND');

When('b').Execute(() => {
  console.log('You can also use function literals!');
});

A sequential shortcut that must be completed within a time limit

When('ctrl+k ctrl+l (500ms)').Execute('EXAMPLE_COMMAND');

A shortcut combining pressed, released and held events

When('1').IsPressed()
  .Then('2').IsHeldFor(1).Seconds()
  .Then('1').IsReleased()
  .Execute('EXAMPLE_COMMAND');

A shortcut implementing various behaviour modifiers

When('ctrl+s').Execute('EXAMPLE_COMMAND')
  .Once()           // shortcut will be deleted after its first use
  .AllowDefault()   // allow default browser behaviour for any shortcuts involved in the chain (disabled by default)
  .InInput();       // allows the shortcut to work in text inputs, which is disabled by default

Shortcut Controllers

const shortcut = When('a').Execute(console.log);

shortcut.pause();   // temporarily stop shortcut from triggering  
shortcut.unpause(); // re-enable shortcut if paused
shortcut.toggle();  // toggle paused state of shortcut
shortcut.trigger(); // programmatically trigger the event handler
shortcut.remove();  // stop shortcut from triggering permanently

Group Controllers

const group = When.newGroup([
  When('a').Execute(console.log),
  When('b').Execute(console.log),
  When('c').Execute(console.log),
]);

group.pause();
group.unpause();
group.toggle();
group.trigger();
group.remove();

Modes

When.modeIs('mode1', [
  When('a').Execute(console.log),
]);

// OR

When().ModeIs('mode1').Then('a').Execute(console.log);

When.setMode('mode1'); // actives all shortcuts with this mode constraint
When.clearMode();      // any shortcuts with mode constraints get disabled

Using the focus system to create a shortcut that only works if a certain element is focused, and to display the focus to the user

<html>
  <head>
    <style>
      .focused {
        background: yellow;
      }
    </style>
  </head>
  <body>
    <!-- if you click within this, the shortcut works! -->
    <div id="shortcut-element" class="when-focus">
      <h1>Title</h1>
      <p>Paragraph.</p>
    </div>

    <!-- if you click within here, the shortcut will stop working -->
    <div>
      <h1>Title</h1>
      <p>Paragraph.</p>
    </div>

    <script src="dist/When.js"></script>
    <script>
      const el = document.getElementById('shortcut-element');

      When.focusIs(el, [
        When('enter').Execute(console.log),
      ]);

      // highlight the focused element
      When.focusChanges((newFocusEl, prevFocusEl) => {
        if (newFocusEl !== null) {
          newFocusEl.classList.add('focused');
        }
        if (prevFocusEl !== null) {
          prevFocusEl.classList.remove('focused');
        }
      });
    </script>
  </body>
</html>

Generate documentation

const table = document.getElementById('doc-table');
When.documentation().forEach((shortcut) => {
  const docLine = document.createElement('tr');
  docLine.innerHTML = `<td>${shortcut.combination}</td><td>${shortcut.command}</td><td>${shortcut.mode}</td>`;
  table.appendChild(docLine);
});

| combination | command | mode | |-|-|-| | ↓a | example_command | mode1 | | ↓num1, ↓num2, ↓num3 (within 500ms) | example_command | | ↓1, ↓2 (hold 500ms), ↑1 | example_command | | ↓ctrl+s | example_command | | ↓enter | example_command |