Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  176] [ 2]  / answers: 1 / hits: 15476  / 6 Years ago, thu, may 24, 2018, 12:00:00

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


More From » angular

 Answers
8

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.


[#54358] Monday, May 21, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ethanc

Total Points: 57
Total Questions: 111
Total Answers: 111

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
;