1

How to focus in mat-input when key press any key on keyboard.

have tried:

@HostListener('document:keydown', ['this.search.nativeElement.focus()'])
Kelvin
  • 179
  • 2
  • 12

2 Answers2

2

Try this:

@HostListener('document:keypress', ['$event'])
    handleKeyboardEvent(event: KeyboardEvent) {
        this.search.nativeElement.focus();
    }
Yair Cohen
  • 2,032
  • 7
  • 13
0

YAY Fixed it and Thank you

 @HostListener('document:keypress', ['$event'])
  handleKeyboardEvent(event: KeyboardEvent) {
    this.search.nativeElement.focus();
    this.searchFocus();
  }

  keyboardFocusOut() : void {
    this.searchText = ""
    this.search.nativeElement.blur();
    this.searchFocusOut();
  }
Kelvin
  • 179
  • 2
  • 12