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

hotkeys-manager

v1.0.3

Published

Keyboard hotkeys manager

Downloads

4

Readme

hotkeys-manager

Executes callback when hotkey commands are pressed.

Install

npm i -D hotkeys-manager

Initialize

import HotkeysManager from 'hotkeys-manager';

// settings these options in the constructor defines these options 
// globaly thorughout command registrations. Though individual options 
// have precedence over those defined globally.  
const options = {
  // defaults
  preventDefault: true,
  once: true,
}
// new ShortcutsManager(Element, Object);
const hotkeys = new HotkeysManager(window, options);
// returns the target Element the eventlisteners are bound to
hotkeys.target; // reference to the element passed in to the HotkeysManager constructor. the `window` object in this case

Register command

The available options consists of these properties.

// optional options
{
  once: Boolean // if global option has been set, this option overrides it
  preventDefault: Boolean // same with preventDefault. This overrides the global value
  data: Any
  groups: Array,
  priority: Number // which command has precendence of being triggered
}

Define a new command with the set method on the shortcuts instance. once and preventDefault options overrides any options defined globally. Data is where you can pass any data which you have access to in the callback.

// shortcuts.set(Array<KeyboardEvent.code>, options);
const open = hotkeys.set(['ControlLeft', 'KeyO'], { 
  once: true,
  preventDefault: true,
  data: 'this command opens something', 
  groups: ['group1'],
  priority: 3
});

Adding callbacks for on and off states. Note that defining on or off callbacks will make it so the global general callback wont be executed for that particular state.

// on
open.on(({ e, Hotkey, on }) => {
  // handle on state
});

// off 
open.off(({ e, Hotkey, on }) => {
  // handle off state
});

Groups

If no groups is provided the command will be stored under a global wildcard group [*]. If you want to register a shortcut to the wildcard group along with other groups, simply add that to the array list as well. Like so.

groups: ['*', 'group1', 'group2']

Enabling certain groups to be listened to.

// returns the same arguments provided, as an array
const groupsArray = hotkeys.enableGroups('group1', 'group2');

Enabling all groups is a special case where it listens for all registered commands. And pick the first registered or the one with the highest priority.

// enable all groups (default)
hotkeys.enableAllGroups();

Listening

To start listening for bound hotkeys call the subscribe method on the instance. This in turn returns an unsubscribe method. These methods add and removes the eventlisteners.

const unsubscribe = hotkeys.subscribe();

Providing a callback function as the subscribe argument you can catch all events here instead of binding on and off on each seperate hotkey.

This callback will be called for both on and off states. The on argument returns a boolean with the value true|false to differentiate between on and off state.

const unsubscribe = hotkeys.subscribe(({ e, Hotkey, on }) => {
  // here we are still able to access the `event` object
  // so it's possible to prevent the default behaviour or 
  // stopPropagation if we so like.
  e.preventDefault();
  // along with the current Hotkey Shortcut instance
  console.log(Hotkey);
});

Executing keycommands programmatically

You can execute a command programmarically by calling the execute function. Call the execute function with the same keys that you would use when you set a new command. And a second optional argument of the state you want to execute.

On some odd occasions you might be required to call one or the other. So provide either the string on or off for the on off states and leave it or provide undefined for sequentially calling on then off.

// hotkeys.execute(keys: Array[, state: String|undefined])

// calling on followed by off
hotkeys.execute(['KeyE']);

// call only 'on' state
hotkeys.execute(['KeyE'], 'on');
// call only 'off' state
hotkeys.execute(['KeyE'], 'off');