Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Keyboard Events

Tags:

Trying to monitor keyboard events with Angular 2 using TypeScript and What is Angular2 way of creating global keyboard shortcuts (a.k.a. hotkeys)? was helpful but tslint (codelyzer) objects with the message

In the "@Component" class decorator you are using the "host" property, this is considered bad practice. Use "@HostBindings", "@HostListeners" property decorator instead.

How do I use the recommended decorators? I'm not sure how the examples in Angular 2: Host binding and Host listening apply to my use case as I am not binding to any DOM elements.

Here is my demo

@Component({   selector: 'my-app',  template: `     <div>       <h2>Keyboard Event demo</h2>       Start typing to see KeyboardEvent values     </div>     <hr />     KeyboardEvent     <ul>       <li>altKey: {{altKey}}</li>       <li>charCode: {{charCode}}</li>       <li>code: {{code}}</li>       <li>ctrlKey: {{ctrlKey}}</li>       <li>keyCode: {{keyCode}}</li>       <li>keyIdentifier: {{keyIdentifier}}</li>       <li>metaKey: {{metaKey}}</li>       <li>shiftKey: {{shiftKey}}</li>       <li>timeStamp: {{timeStamp}}</li>       <li>type: {{type}}</li>       <li>which: {{which}}</li>     </ul>       `,   host: { '(window:keydown)': 'keyboardInput($event)' }   /*   In the "@Component" class decorator you are using the "host" property, this is considered bad practice.    Use "@HostBindings", "@HostListeners" property decorator instead.   */  }) export class App {    /* a few examples */   keyboardEvent: any;   altKey: boolean;   charCode: number;   code: string;   ctrlKey: boolean;   keyCode: number;   keyIdentifier: string;   metaKey: boolean;   shiftKey: boolean;   timeStamp: number;   type: string;   which: number;    keyboardInput(event: any) {     event.preventDefault();     event.stopPropagation();      this.keyboardEvent = event;     this.altKey = event.altKey;     this.charCode = event.charCode;     this.code = event.code;     this.ctrlKey = event.ctrlKey;     this.keyCode = event.keyCode;     this.keyIdentifier = event.keyIdentifier;     this.metaKey = event.metaKey;     this.shiftKey = event.shiftKey;     this.timeStamp = event.timeStamp;     this.type = event.type;     this.which = event.which;   }  } 

https://plnkr.co/edit/Aubybjbkp7p8FPxqM0zx

like image 386
james sloan Avatar asked Apr 18 '16 13:04

james sloan


People also ask

What is KeyUp and KeyDown in angular?

1. The KeyDown event is triggered when the user presses a Key. 2. The KeyUp event is triggered when the user releases a Key.

What is the type of $event angular?

Angular includes $event that contains the information about an event. The type of $event depends on the target event, e.g., if the target event is a native DOM element event, then it is an object. A component should define the onShow(event) method where the type of the parameter can be KeyboardEvent, MouseEvent, etc.

Why is KeyCode deprecated?

KeyCode was deprecated because in practice it was “inconsistent across platforms and even the same implementation on different operating systems or using different localizations.” The new recommendation is to use key or code .


1 Answers

import {HostListener} from '@angular/core';  @HostListener('window:keydown', ['$event']) handleKeyDown(event: KeyboardEvent) {   // event.key === 'ArrowUp' } 
  • @HostBindings('attr.foo') foo = 'bar' is to bind values from your component instance to the host element like class, attributes, properties or styles.
like image 64
Günter Zöchbauer Avatar answered Nov 11 '22 13:11

Günter Zöchbauer