Search code examples
angularangular2-directivesangular-directivekeycode

How to get a keyCode for *focus* HostListener


I have a directive which set a focus style to the active element in the HTML DOM. For now, it's work fine but I need to do this logic only when I press the tab button.

@HostListener('focus', ["$event"])
    onFocus(event: KeyboardEvent) {
        console.log("Focus called from HostListener");
    }

And I don't see in the event object the keyCode property.

How can I get this keyCode for this event?


Solution

  • HTML:

    <input (keydown.Tab)="onKey($event)">
    

    TS:

    onKey(event:any){
     //Do logic
    }