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

@unrest/vue-mousetrap

v0.0.13

Published

This provides an easy way to add keyboard shortcuts to any vue component via [mixin](#mixin-usage) or [composition api](#composition-api-usage). Why not start with the [interactive demo with code examples](https://chriscauley.github.io/unrest-vue-mousetra

Downloads

3

Readme

@unrest/vue-mousetrap

This provides an easy way to add keyboard shortcuts to any vue component via mixin or composition api. Why not start with the interactive demo with code examples? This package is a wrapper around mousetrap so additional information (key aliases, etc) can be found there.

Installation

npm install --save @unrest/vue-mousetrap should install this package from npm. If that doesn't work for you or if there's another distribution method you'd prefer please open an issue on github.

Composition API Usage

<script>
import { ref } from 'vue'
import Mousetrap from '@unrest/vue-mousetrap'

export default {
  setup(props) {
    const game = {
      paused: ref(false),
      // pause, unpause, move, showHelp, hideHelp, and toggleCheat go here
    }

    Mousetrap.use(() => {
      if (game.paused.value) { // 1
        return {
          esc: game.unpause
        }
      }
      return {
        esc: game.pause,
        'space,x,enter': game.useItem, // 2
        'up,down,left,right': (event) => game.move(event.key), // 3
        '?': { // 4
          keydown: game.showHelp,
          keyup: game.hideHelp,
        },
        'up up down down left right left right b a,mod+shift+q': game.toggleCheat // 5
      }
    })

    return { paused, game }
  },
}
</script>

Notes:

  1. Because game.paused is a ref and the action_map argument for Mousetrap.use(action_map) is a function, hotkeys will be recalculated and rebound every time game.paused.value changes. If actions are unchanging, then actions can be an object instead of a function (see mixin or action map for example).

  2. Multiple hotkeys can be specified if separated by a comma ,. This delimiter can be change by using Mousetrap.use(action_map, { delimeter: '&&&' })

  3. The first argument passed into an action is the native keyboard event. Also, arrow functions are the best.

  4. The action can either be a function or a an object with keys { keydown, keyup, repeat, global }. See full details below.

  5. Any valid mousetrap sequence will work for the keys. In this case the konami code or pressing ctrl+shift+q (command+shift+q on a mac) will toggleCheat.

Mixin Usage

Use the default import from this package and add a mousetrap computed property to a component to enable shortcuts. component.mousetrap should return an object with a shortcut string as the key and an action or action object as the value.

<script>
import Mousetrap from '@unrest/vue-mousetrap'

export default {
  mixins: [Mousetrap.Mixin],
  computed: {
    mousetrap() {
      if (this.paused) { // 1
        return {
          esc: this.unpause
        }
      }
      return {
        esc: this.pause,
        'space,x,enter': this.useItem, // 2
        'up,down,left,right': (event) => this.move(event.key), // 3
        '?': { // 4
          keydown: this.showHelp,
          keyup: this.hideHelp,
        },
        'up up down down left right left right b a,mod+shift+q': this.toggleCheat // 5
      }
    }
  },
  methods: {
    ... // pause, unpause, move, showHelp, hideHelp, and toggleCheat go here
  }
}

// ...

Notes:

  1. Because this is a computed property, hotkeys will be reloaded any time this.paused is changed. Other hotkeys (arrows and help menu) will not be available while paused.

  2. Multiple hotkeys can be specified if separated by a comma ,. This delimiter can be set using mixin config options below.

  3. The first argument passed into an action is the native keyboard event. Also, arrow functions are the best.

  4. The action can either be a function or a an object with keys { keydown, keyup, repeat, global }. See full details below.

  5. Any valid mousetrap sequence will work for the keys. In this case the konami code or pressing ctrl+shift+q (command+shift+q on a mac) will toggleCheat.

Mixin Config options

If you want to use a different delimiter or namespace for the mousetrap, the mixin is configurable:

  mixins: [MousetrapMixin.config({ delimeter: '&&&', namespace: 'myHotkeys' })],
  computed: {
    myHotkeys: {
      ', &&& enter': this.doSomething
    }
  }

Action Map

If the action is specified as a function, the hotkey will default to executing that function on keydown. So esc: () => this.paused = false and esc: { keydown: () => this.paused = false } are equivalent. Full behavior can be customized by passing in an object instead. Available options are

  • keydown: Function(event)

  • keyup: Function(event)

  • keypress: Function(event)

  • repeat: Function(event) - Will be fired when event.repeat === true (ie user is holding down a key). If keydown === undefined, keydown will default to repeat.

  • global: Boolean - By default mousetrap will ignore hotkeys on input, textarea, and select elements. Setting global to true will capture these events (ie you want ctrl+s be captured even when a text area is focused).

  mousetrap: {
    a: {
      keydown: this.doSomething,
      keypress: this.doSomethingElse, // TBH I don't know what keypress is used for
      repeat: this.doSomethingALot,
      keyup: this.stopSomething,
      global: true, // all the above work anywhere, including inputs
    },
    b: () => this.keyPressed('b'), // a function will be interpreted as { keydown: function }
    'up,down,left,right': {
      repeat: this.move, // holding down arrows spams repeat, keydown is this.move
    },
    'ctrl+s': {
       keydown: this.save,
       global: true, // save even in textarea
    },
    '?': {
      keydown: this.showHelp,
      keyup: this.hideHelp,
    },
  }