Angular 2 HostListener keypress detect escape key?

前端 未结 6 1123
温柔的废话
温柔的废话 2020-12-24 01:47

I am using the following method to detect keypresses on a page. My plan is to detect when the Escape key is pressed and run a method if so. For the moment I am just attempti

6条回答
  •  天命终不由人
    2020-12-24 02:24

    Modern approach, event.key == "Escape"

    The old alternatives (.keyCode and .which) are Deprecated.

    @HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) {
        if (event.key === "Escape") {
            // Do things
        }
    }
    

提交回复
热议问题