admin管理员组

文章数量:1220535

I need to remove focus from input text field from user with the ESC key press. I don't know how to do it with text input fields. Below code works perfectly to detect the ESC key is pressed.

// Triggered Listener to detect event raised from DOM.
@HostListener('document:keydown', ['$event']) onKeydownHandler(event: 
KeyboardEvent) {
 if (event.keyCode === 27) {
    return false;
  }
}

Anyone can help how to do?? Thanks

I need to remove focus from input text field from user with the ESC key press. I don't know how to do it with text input fields. Below code works perfectly to detect the ESC key is pressed.

// Triggered Listener to detect event raised from DOM.
@HostListener('document:keydown', ['$event']) onKeydownHandler(event: 
KeyboardEvent) {
 if (event.keyCode === 27) {
    return false;
  }
}

Anyone can help how to do?? Thanks

Share Improve this question asked May 24, 2018 at 13:11 Vijay Kumar YadavVijay Kumar Yadav 3171 gold badge3 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

You can call the blur method of the input field in the handler of the keydown.escape event:

<input #txtInput (keydown.escape)="txtInput.blur()">

See this stackblitz for a demo.

本文标签: javascriptHow to remove focus from text field when user presses ESC angular 5Stack Overflow