Can a HostListener be set on a specific button?

I would like to call some method when the ESC button is pressed . I did it this way:

@HostListener('window:keydown', ['$event'])
clickEscape(event: KeyboardEvent) {
    //if ESC was pressed
    if(event.keyCode === 27) {
        this.someMethod();
    }
}

      

But the clickEscape method will be called every time the user presses any button on the keyboard. And my question is:

Is it possible to set the HostListener so that the method is only called when the ESCape button is detected? I was thinking of something in style:

@HostListener('window:keydown["ESCAPE"]')
someMethod() {
    //...
}

      

Is there a similar event?

+3


source to share


2 answers


You can actually use keydown.escape

:



@HostListener('window:keydown.escape')
someMethod() {
    //...
}

      

+2


source


I want to use @hostlistener on html button to add and remove class from div element. Is it possible?



0


source







All Articles