Skip to content

Commit

Permalink
docs(core): showcase host listener support for specific keys in events (
Browse files Browse the repository at this point in the history
#44833)

PR Close #44833
  • Loading branch information
macjohnny authored and thePunderWoman committed Jan 31, 2022
1 parent b51d111 commit 25f8305
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/core/src/metadata/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,19 +879,20 @@ export interface HostListener {
*
* ```
*
* The following example registers another DOM event handler that listens for key-press events.
* The following example registers another DOM event handler that listens for `Enter` key-press
* events on the global `window`.
* ``` ts
* import { HostListener, Component } from "@angular/core";
*
* @Component({
* selector: 'app',
* template: `<h1>Hello, you have pressed keys {{counter}} number of times!</h1> Press any key to
* increment the counter.
* template: `<h1>Hello, you have pressed enter {{counter}} number of times!</h1> Press enter key
* to increment the counter.
* <button (click)="resetCounter()">Reset Counter</button>`
* })
* class AppComponent {
* counter = 0;
* @HostListener('window:keydown', ['$event'])
* @HostListener('window:keydown.enter', ['$event'])
* handleKeyDown(event: KeyboardEvent) {
* this.counter++;
* }
Expand All @@ -900,6 +901,14 @@ export interface HostListener {
* }
* }
* ```
* The list of valid key names for `keydown` and `keyup` events
* can be found here:
* https://www.w3.org/TR/DOM-Level-3-Events-key/#named-key-attribute-values
*
* Note that keys can also be combined, e.g. `@HostListener('keydown.shift.a')`.
*
* The global target names that can be used to prefix an event name are
* `document:`, `window:` and `body:`.
*
* @Annotation
* @publicApi
Expand Down

0 comments on commit 25f8305

Please sign in to comment.