
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
vue-shortkey
Advanced tools
A plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.
Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.
npm install vue-shortkey --save
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.
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>
<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
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" />
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>
Use the modifier native
to catch the event.
<my-component v-shortkey="['ctrl', 'alt', 'o']" @shortkey.native="theAction()"></my-component>
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>
You can avoid shortcuts within fields if you really need it. This can be done in two ways:
<textarea v-shortkey.avoid></textaea>
Vue.use('vue-shortkey', { prevent: ['input', 'textarea'] })
Vue.use('vue-shortkey', { prevent: ['.my-class-name', 'textarea.class-of-textarea'] })
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>
npm test
FAQs
A plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.
The npm package vue-shortkey receives a total of 37,609 weekly downloads. As such, vue-shortkey popularity was classified as popular.
We found that vue-shortkey demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.