vue-mousetrap

just a basic wrapper to add mousetrap to vue instance
12,045 16
Install
npm install vue-mousetrap
Latest Version:1.0.5
License:MIT
Last Updated:Jan 30, 2026
Links: GitHub  ·  npm
Maintainer: g33kio

vue-mousetrap

A vue plugin to add mousetrap to the Vue object.

Installing

Install using NPM

npm install vue-mousetrap --save

Install using Yarn

yarn add vue-mousetrap

Usage

The loading of the plugin is different between Vue 2 and Vue 3 in the main.js file. Once loaded access mousetrap in components is the same for both versions.

Vue 2

import Vue from 'vue'
import VueMousetrapPlugin from 'vue-mousetrap/vue2'
Vue.use(VueMousetrapPlugin)

Vue 3

Usage with options API
import { createApp } from 'vue'
import App from './App.vue'
import VueMousetrapPlugin from 'vue-mousetrap'

const app = createApp(App)

app.use(VueMousetrapPlugin)

app.mount('#app')
Usage with composition API
app.use(VueMousetrapPlugin).provide('mousetrap', app.config.globalProperties.$mousetrap).mount('#app')

Vue 2 and Vue 3 Access in Components

Usage with options API
<script>
  export default {
    name: 'mousetrap-demo',
    mounted() {
      // single keys
      this.$mousetrap.bind('4', this.logIt);
      this.$mousetrap.bind("?", this.logIt);
      this.$mousetrap.bind('esc', this.logIt, 'keyup');

      // combinations
      this.$mousetrap.bind('command+shift+k', this.logIt);

      // map multiple combinations to the same callback
      this.$mousetrap.bind(['command+k', 'ctrl+k'], this.logIt);

      // gmail style sequences
      this.$mousetrap.bind('g i', this.logIt);
      this.$mousetrap.bind('* a', this.logIt);

      // konami code!
      this.$mousetrap.bind('up up down down left right left right b a enter', this.logIt);
    },
    methods: {
      logIt(e) {
        console.log(e);
        return false;
      }
    }
  };
</script>
Usage with composition API
setup() {
    const mousetrap = inject('mousetrap')

    onMounted(() => {
        mousetrap.bind(['command+k', 'ctrl+k'], () => {
            console.log("Yikes")
        });
    })

    return {};
},

Related Packages

vue-mouseless

A tiny vue directive to bind shortcut keys using mousetrap

4,127 2
@4rk/vue-mousetrap

Mousetrap directive for Vue

2,602 2
tinykeys

A tiny (~1KB) & modern library for keybindings.

10,256,877 4,102
vue

The progressive JavaScript framework for building modern web UI.

588,019,132 54,801
vue
@vueuse/shared
372,738,348 22,398

Version History

Version Released License
1.0.5 MIT
1.0.4 MIT
1.0.3 MIT
1.0.2 MIT
1.0.1 MIT