1
2 content_copy
3
4 import { HostListener, Component } from "@angular/core";
5
6@Component({
7 selector: 'app',
8 template: `<h1>Hello, you have pressed keys {{counter}} number of times!</h1> Press any key to
9increment the counter.
10 <button (click)="resetCounter()">Reset Counter</button>`
11})
12class AppComponent {
13 counter = 0;
14 @HostListener('window:keydown', ['$event'])
15 handleKeyDown(event: KeyboardEvent) {
16 this.counter++;
17 }
18 resetCounter() {
19 this.counter = 0;
20 }
21}
22