Reputation: 2522
I'm looking for a way to bind a function to my whole page (when a user presses a key, I want it to trigger a function in my component.ts)
It was easy in AngularJS with a ng-keypress
but it does not work with (keypress)="handleInput($event)"
.
I tried it with a div wrapper on the whole page but it doesn't seem to work. it only works when the focus is on it.
<div (keypress)="handleInput($event)" tabindex="1">
Upvotes: 176
Views: 190674
Reputation: 76
Be aware document:keypress
is deprecated. We should use document:keydown
instead.
Link: https://developer.mozilla.org/en-US/docs/Web/API/Element/keypress_event
Upvotes: 2
Reputation: 71
I struggeled a little bit on this one so; tshoemake provided the correct answer that also works for F keys. To prevent the F key from executing it's original browser action, like search, also call the preventDefault() method.
add the following includes
import { Component, HostListener } from '@angular/core';
and
@HostListener('document:keydown', ['$event'])
handleTheKeyboardEvent(event: KeyboardEvent) {
switch (event.key) {
case "F2":
this.myF2Action();
event.preventDefault();
break;
}
}
Upvotes: 2
Reputation: 32716
I think this does the best job
https://angular.io/api/platform-browser/EventManager
for instance in app.component
constructor(private eventManager: EventManager) {
const removeGlobalEventListener = this.eventManager.addGlobalEventListener(
'document',
'keypress',
(ev) => {
console.log('ev', ev);
}
);
}
Upvotes: 2
Reputation: 1351
Just to add to this in 2019 w Angular 8,
instead of keypress I had to use keydown
@HostListener('document:keypress', ['$event'])
to
@HostListener('document:keydown', ['$event'])
Working Stacklitz
Upvotes: 28
Reputation: 214017
I would use @HostListener decorator within your component:
import { HostListener } from '@angular/core';
@Component({
...
})
export class AppComponent {
@HostListener('document:keypress', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
this.key = event.key;
}
}
There are also other options like:
host property within @Component
decorator
Angular recommends using @HostListener
decorator over host property https://angular.io/guide/styleguide#style-06-03
@Component({
...
host: {
'(document:keypress)': 'handleKeyboardEvent($event)'
}
})
export class AppComponent {
handleKeyboardEvent(event: KeyboardEvent) {
console.log(event);
}
}
renderer.listen
import { Component, Renderer2 } from '@angular/core';
@Component({
...
})
export class AppComponent {
globalListenFunc: Function;
constructor(private renderer: Renderer2) {}
ngOnInit() {
this.globalListenFunc = this.renderer.listen('document', 'keypress', e => {
console.log(e);
});
}
ngOnDestroy() {
// remove listener
this.globalListenFunc();
}
}
Observable.fromEvent
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import { Subscription } from 'rxjs/Subscription';
@Component({
...
})
export class AppComponent {
subscription: Subscription;
ngOnInit() {
this.subscription = Observable.fromEvent(document, 'keypress').subscribe(e => {
console.log(e);
})
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
Upvotes: 307
Reputation: 4683
yurzui's answer didn't work for me, it might be a different RC version, or it might be a mistake on my part. Either way, here's how I did it with my component in Angular2 RC4 (which is now quite outdated).
@Component({
...
host: {
'(document:keydown)': 'handleKeyboardEvents($event)'
}
})
export class MyComponent {
...
handleKeyboardEvents(event: KeyboardEvent) {
this.key = event.which || event.keyCode;
}
}
Upvotes: 22
Reputation: 1088
If you want to perform any event on any specific keyboard button press, in that case, you can use @HostListener. For this, you have to import HostListener in your component ts file.
import { HostListener } from '@angular/core';
then use below function anywhere in your component ts file.
@HostListener('document:keyup', ['$event'])
handleDeleteKeyboardEvent(event: KeyboardEvent) {
if(event.key === 'Delete')
{
// remove something...
}
}
Upvotes: 7