It is possible to add global event listeners in your Components/Directives with HostListener
. Angular will take care of unsubscribing once your directive is destroyed.
@Directive({
selector: '[rightClicker]'
})
export class ShortcutsDirective {
@HostListener('window:keydown.ArrowRight')
doImportantThings() {
console.log('You pressed right');
}
}
You can have multiple bindings:
@HostListener('window:keydown.ArrowRight')
@HostListener('window:keydown.PageDown')
next() {
console.log('Next')
}
You can also pass params:
@HostListener('window:keydown.ArrowRight', '$event.target')
next(target) {
console.log('Pressed right on this element: ' + target)
}