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

@violentmonkey/shortcut

v1.4.4

Published

Register a shortcut for a function

Downloads

335

Readme

VM.shortcut

NPM License jsDocs.io

Register a shortcut for a function.

This is a helper script for Violentmonkey.

👉 Playground: https://violentmonkey.github.io/vm-shortcut/

Usage

Importing

  1. Use in a userscript:

    // ...
    // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
    // ...
    
    const { register, ... } = VM.shortcut;
  2. Use as a module:

    $ yarn add @violentmonkey/shortcut
    import { register, ... } from '@violentmonkey/shortcut';

Registering Shortcuts

  1. Register a shortcut:

    import { register } from '@violentmonkey/shortcut';
    
    register('c-i', () => {
      console.log('You just pressed Ctrl-I');
    });
    
    // shortcuts will be enabled by default
  2. Enable or disable all shortcuts:

    import { enable, disable } from '@violentmonkey/shortcut';
    
    disable();
    // ...
    enable();
  3. Key sequences:

    import { register } from '@violentmonkey/shortcut';
    
    register('c-a c-b', () => {
      console.log('You just pressed Ctrl-A Ctrl-B sequence');
    });
  4. Handle keys with custom listeners (e.g. use with text editor like TinyMCE):

    import { handleKey } from '@violentmonkey/shortcut';
    
    function onKeyDown(e) {
      handleKey(e);
    }
    
    addMyKeyDownListener(onKeyDown);

Advanced Usage

The usage above is with the default keyboard service. However you can use the KeyboardService directly to get full control of the class:

import { KeyboardService } from '@violentmonkey/shortcut';

const service = new KeyboardService();
// Or pass options
const service = new KeyboardService({
  sequenceTimeout: 500,
});

service.enable();

service.register('c-i', () => {
  console.log('You just pressed Ctrl-I');
});

// Only register the following key when `disableThisKey` is false
service.register(
  'g g',
  () => {
    console.log('Now disableThisKey is false and you pressed `g g`');
  },
  {
    condition: '!disableThisKey',
  }
);

// Update `disableThisKey` on different conditions
// Note: these callbacks are just demos, you need to implement them by yourself!!!
onOneCondition(() => {
  service.setContext('disableThisKey', true);
});
onAnotherCondition(() => {
  service.setContext('disableThisKey', false);
});

// Disable the shortcuts and unbind all events whereever you want
service.disable();

// Reenable the shortcuts later
service.enable();

API

jsDocs.io

Key Definition

A key sequence is a space-separated list of combined keys. Each combined key is composed of zero or more modifiers and exactly one base key in the end, concatenated with dashes (-). The modifiers are always case-insensitive and can be abbreviated as their first letters.

Here are some valid examples:

ctrl-alt-c
ctrl-a-c
c-a-c

Possible modifiers are:

  • c, ctrl, control
  • s, shift
  • a, alt
  • m, meta, cmd
  • cm, ctrlcmd

There is one special case, ctrlcmd for ctrl on Windows and cmd for macOS, so if we register ctrlcmd-s to save something, the callback will be called when ctrl-s is pressed on Windows, and when cmd-s is pressed on macOS. This is useful to register cross-platform shortcuts.

Condition Syntax

  • conditionA - when conditionA is truthy
  • !conditionB - when conditionB is falsy
  • conditionA && conditionB - when both conditionA and conditionB are truthy

For more complicated cases, it's recommended to handle the logic in a function and store the result as a simple condition to the context.