-
-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vuejs version of Hotkeys #115
Comments
Mind sharing more about what you're trying to achieve? It would help if you can give us more insight into how deep do you want the lib to integrate with your project. I'm currently using this lib in my project, but I'm not doing anything too complicated and it's just sitting on top of what I have. If that's what you want, feel free to check out the file where I'm using it. Cheers! |
You can use the import hotkeys from "hotkeys-js";
export default {
name: "ComponentWithHotkey",
mounted() {
hotkeys("a", () => this.hotkeyPressed());
},
beforeDestroy() {
hotkeys.unbind("a");
},
methods: {
hotkeyPressed() {
this.$emit("increment");
}
}
}; |
The problem is that the imported Unfortunately, if re-binding the same keys on a new component doesn't introduce bugs, you'll have a memory leak that will be hard to track. @jaywcjlove |
@scambier Even if you had a separate instance of the // hypothetical API for creating a hotkeys instance
import { makeHotkeysInstance } from "hotkeys-js";
export default {
name: "ComponentWithHotkey",
mounted() {
this.hotkeys = makeHotkeysInstance();
this.hotkeys("a", () => this.hotkeyPressed());
},
beforeDestroy() {
// still requires cleanup or else the arrow function
// will remain bound to the window's keydown event
this.hotkeys.destroy();
},
...
}; |
You're right, I'm not sure what I had in mind. But your example shows a nice way to do it: you don't have to keep an up-to-date list of |
Sorry for responding to an old thread, but I took a different route by binding hotkeys to the Vue instance prototype. And only clean up using the scope functions in my components. This way the cleanup is simple and hotkeys is called only once. Before calling new Vue() attach hotkeys: And in my components: export default {
data: {
hotKeyScope: 'some-usefull-name'
},
created() {
this.$keys('enter', this.hotKeyScope, () => { /*...*/ })
},
beforeDestroy() {
this.$keys.deleteScope(this.hotKeyScope);
}
} I've not tested all use/edge-cases, but worked well enough in my apps. My guess is that if you have multiple competing scopes on the same page, you'll have to manage these yourself. |
Hi there, I made a composition-api version of this and published as |
Hello,
Thanks for a wonderful keyboard binding library, I was trying to integrate this library in my vue project I and can't.
Does any one tried to implement in their vuejs project? If someone tried, guide me how to integrate it, that would be amazing.
Thanks!
Rutvij
The text was updated successfully, but these errors were encountered: