GithubHelp home page GithubHelp logo

hhy5277 / vue-shortkey Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fgr-araujo/vue-shortkey

0.0 1.0 0.0 1.55 MB

Vue-ShortKey - plugin for Vue.js

License: MIT License

JavaScript 100.00%

vue-shortkey's Introduction

vue-shortkey logo

CircleCI status npm version npm MIT Licence

Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.

Install

npm install vue-shortkey --save

Usage

Vue.use(require('vue-shortkey'))

Add the shortkey directive to the elements that accept the shortcut. The shortkey must have explicitly which keys will be used.

Running functions

The code below ensures that the key combination ctrl + alt + o will perform the 'theAction' method.

<button v-shortkey="['ctrl', 'alt', 'o']" @shortkey="theAction()">Open</button>

The function in the modifier @shortkey will be called repeatedly while the key is pressed. To call the function only once, use the once modifier

<button v-shortkey.once="['ctrl', 'alt', 'o']" @shortkey="theAction()">Open</button>

Multi keys

<button v-shortkey="{up: ['arrowup'], down: ['arrowdown']}" @shortkey="theAction">Joystick</button>

... and your method will be called with the key in the parameter

methods: {
  theAction (event) {
    switch (event.srcKey) {
      case 'up':
        ...
        break
      case 'down':
        ...
        break

Setting the focus

You can point the focus with the shortcut easily. The code below reserves the ALT + I key to set the focus to the input element.

<input type="text" v-shortkey.focus="['alt', 'i']" v-model="name" />

Push button

Sometimes you may need a shortcut works as a push button. It calls the function one time until you release the shortcut. When you release the shortcut, it call the same function again like a toggle. In these cases, insert the "push" modifier.

The example below shows how to do this

<tooltip v-shortkey.push="['f3']" @shortkey="toggleToolTip"></tooltip>

Using on a component

Use the modifier native to catch the event.

 <my-component v-shortkey="['ctrl', 'alt', 'o']" @shortkey.native="theAction()"></my-component>

Key list

Key Shortkey Name
Delete del
Backspace backspace
Insert insert
NumLock numlock
CapsLock capslock
Pause pause
ContextMenu contextmenu
ScrollLock scrolllock
BrowserHome browserhome
MediaSelect mediaselect
Shift shift
Control ctrl
Alt alt
Alt Graph altgraph
Super (Windows or Mac Cmd) meta
Arrow Up arrowup
Arrow Down arrowdown
Arrow Left arrowleft
Arrow Right arrowright
Enter enter
Escape esc
Tab tab
Space space
Page Up pageup
Page Down pagedown
Home home
End end
A - Z a-z
0-9 0-9
F1-F12 f1-f12

You can make any combination of keys as well as reserve a single key.

<input type="text" v-shortkey="['q']" @shortkey="foo()"/>
<button v-shortkey="['ctrl', 'p']" @shortkey="bar()"></button>
<button v-shortkey="['f1']" @shortkey="help()"></button>
<textarea v-shortkey="['ctrl', 'v']" @shortkey="dontPaste()"></textarea>

Avoided fields

You can avoid shortcuts within fields if you really need it. This can be done in two ways:

  • Preventing a given element from executing the shortcut by adding the v-shortkey.avoid tag in the body of the element
<textarea v-shortkey.avoid></textaea>
  • Generalizing type of element that will not perform shortcut. To do this, insert a list of elements in the global method.
Vue.use('vue-shortkey', { prevent: ['input', 'textarea'] })
  • Or even by class
Vue.use('vue-shortkey', { prevent: ['.my-class-name', 'textarea.class-of-textarea'] })

Other uses

With the dynamism offered by Vue, you can easily create shortcuts dynamically

<li v-for="(ctx, item) in items">
  <a
    href="https://vuejs.org"
    target="_blank"
    v-shortkey="['f' + (item + 1)]"
    @shortkey="testa(item)"
    @click="testa()">
      F {{ item }}
  </a>
</li>

Unit Test

npm test

vue-shortkey's People

Contributors

co-kevin avatar fgr-araujo avatar francois2metz avatar michaelschufi avatar posva avatar splode avatar sunya9 avatar tobiasdierich avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.