101

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 attempting to log which key is pressed. However the Escape key is never detected.

@HostListener('document:keypress', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
  console.log(event);
  let x = event.keyCode;
  if (x === 27) {
      console.log('Escape!');
  }
}
Jeremy P
  • 1,309
  • 2
  • 13
  • 22

6 Answers6

207

Try it with a keydown or keyup event to capture the Esc key. In essence, you can replace document:keypress with document:keydown.escape:

@HostListener('document:keydown.escape', ['$event']) onKeydownHandler(event: KeyboardEvent) {
    console.log(event);
}
Sawant
  • 4,321
  • 1
  • 27
  • 30
43

It worked for me using the following code:

const ESCAPE_KEYCODE = 27;

@HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) {
    if (event.keyCode === ESCAPE_KEYCODE) {
        // ...
    }
}

or in shorter way:

@HostListener('document:keydown.escape', ['$event']) onKeydownHandler(evt: KeyboardEvent) {
    // ...
}
piecioshka
  • 4,752
  • 2
  • 20
  • 29
Gaurav Pandvia
  • 805
  • 7
  • 13
  • 4
    Note that KeyCode is being [deprecated](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode), you can replace with [event.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) and match against the string "Escape". No need to use codes, having the side effect of making the code easy to read. – Tony Mar 29 '18 at 09:18
  • 1
    Nice `document:keydown.escape` so we do not need to do that `27` check. Thanks. – LeOn - Han Li Jul 12 '18 at 22:30
32

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
    }
}
Gibolt
  • 42,564
  • 15
  • 187
  • 127
  • 2
    Doesn't work in IE 11, where the key is called "Esc": if (event.key === "Escape" || event.key === "Esc") – akcasoy Jul 30 '18 at 16:25
  • 2
    Good catch! To be fair though, the title is 'Modern' :D – Gibolt Jul 30 '18 at 16:45
  • 2
    I was having problems consistently capturing char code 13 (enter key) when using a barcode scanner with an angular component. Changing from keypress to keydown resolved this for me. – James Blake Jul 12 '19 at 17:13
10

In my case (with Angular 10) keydown.escape works fine to track event escape by console.log():

@HostListener('keydown.escape')
fn() {
 console.log();
}

But Angular change detector work fine only with keyup.escape.

ktretyak
  • 27,251
  • 11
  • 40
  • 63
5

Angular makes this easy with the @HostListener decorator. This is a function decorator that accepts an event name as an argument. When that event gets fired on the host element it calls the associated function.

@HostListener('document:keydown.escape', ['$event']) onKeydownHandler(event: 
    KeyboardEvent) {
    this.closeBlade();
   }
Mei
  • 305
  • 2
  • 7
3

keydown and keyup seem to work: http://embed.plnkr.co/VLajGbWhbaUhCy3xss8l/

Lucas Tétreault
  • 953
  • 5
  • 15