https://github.com/idimetrix/vue2-hotkeys
Vue 2.x directive for binding hotkeys to components
https://github.com/idimetrix/vue2-hotkeys
Last synced: 4 months ago
JSON representation
Vue 2.x directive for binding hotkeys to components
- Host: GitHub
- URL: https://github.com/idimetrix/vue2-hotkeys
- Owner: idimetrix
- Created: 2020-01-06T20:00:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-07T16:47:09.000Z (about 2 years ago)
- Last Synced: 2025-01-05T08:03:57.284Z (5 months ago)
- Language: JavaScript
- Size: 851 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# vue2-hotkeys
Vue 2.x directive for binding hotkeys to components
## Play with it
[https://github.com/idimetrix/vue2-hotkeys](https://github.com/idimetrix/vue2-hotkeys)
## Install
```bash
$ npm i --save vue2-hotkeys
```## Usage
```javascript
import Vue from 'vue'
import VueHotkeys from 'vue2-hotkeys'Vue.use(VueHotkeys)
``````vue
Press `ctrl + esc` to toggle me! Hold `enter` to hide me!
Press `shift + esc` to toggle me! Hold `1` to hide me!
Press `alt + esc` to toggle me! Hold `2` to hide me!
export default {
name: "App",
data() {
return {
visible1: true,
visible2: true,
visible3: true,
keymap1: {
"ctrl+esc": () => (this.visible1 = !this.visible1),
enter: {
keydown: () => (this.visible1 = false),
keyup: () => (this.visible1 = true)
}
},
keymap2: {
"shift+esc": () => (this.visible2 = !this.visible2),
"1": {
keydown: () => (this.visible2 = false),
keyup: () => (this.visible2 = true)
}
},
keymap3: {
"alt+esc": () => (this.visible3 = !this.visible3),
"2": {
keydown: () => (this.visible3 = false),
keyup: () => (this.visible3 = true)
}
}
};
}
};```
## Event Handler
- keydown (as default)
- keyup## Key Combination
Use one or more of following keys to fire your hotkeys.
- ctrl
- alt
- shift
- meta (windows / command)## Modifiers
### prevent
Add the prevent modifier to the directive to prevent default browser behavior.
```vue
Press `ctrl + esc` to toggle me! Hold `enter` to hide me!
```
### stop
Add the stop modifier to the directive to stop event propagation.
```vue
Enter characters in editable areas doesn't trigger any hotkeys.
```
## Key Code Alias
The default key code map is based on US standard keyboard.
This ability to provide their own key code alias for developers who using keyboards with different layouts. The alias name must be a **single character**.### Definition
```javascript
import Vue from 'vue'
import VueHotkeys from 'vue2-hotkeys'Vue.use(VueHotkeys, {
name: 'hotkeys',
alias: {
'9': 57 // the key code of character '9'
}
})
```### Template
```vue
export default {
foo () {
console.log('Bar!')
},
computed: {
keymap () {
return {
'9': foo
}
}
}
}```
## License
MIT