brtnshrdr / angular2-hotkeys

Keyboard shortcuts for Angular 2 apps
MIT License
203 stars 95 forks source link

how to remove all listener on destroy #152

Closed indraraj26 closed 2 years ago

indraraj26 commented 3 years ago

I have created directive that add the key and listen to it, that directive gets applied to my component. How can i ensure about that those listener are destroyed when my component is destroyed.

this._hotkeysService.add(new Hotkey(['meta+shift+g', 'alt+shift+s'], (event: KeyboardEvent, combo: string): ExtendedKeyboardEvent => {
    console.log('Combo: ' + combo); // 'Combo: meta+shift+g' or 'Combo: alt+shift+s'
    let e: ExtendedKeyboardEvent = event;
    Output emitter .emit()
    e.returnValue = false; // Prevent bubbling
    return e;
}));
pepijnmm commented 3 years ago

ngOnDestroy() { this.hotkeysService.remove(this._hotkeysService.get(['meta+shift+g', 'alt+shift+s'])); } would something like this not just work?

indraraj26 commented 3 years ago

I tried it is working, I have read somewhere you will have to pass same reference in order to prevent memory leak.

Playground: https://stackblitz.com/edit/angular-ivy-ycqk3q?file=src%2Fapp%2Fmy-hotkey.directive.ts