1

I am running Angular 7.x - I have the following ngOnChanges lifeCycle hook and I need to determine if the user has pressed the [ENTER] key and if so pass this boolean value to the processLinks() below as a boolean value - what is the best way to do this as I am unsure of how this is done?

    ngOnChanges(changes: SimpleChanges){
        console.log(changes);
        if(changes.text) {
            this.process();
        }
    }
Zabs
  • 13,852
  • 45
  • 173
  • 297

3 Answers3

1

Angular has a built in way of detecting enter key press

<input (keyup.enter)="testFun()">
Faizal Hussain
  • 1,551
  • 2
  • 10
  • 18
0

SimpleChanges describes only variables, existing in component. To handle 'Enter' key create handler in view for event 'keypress' and check keyCode to be equal 'Enter' key code

Alex Vovchuk
  • 2,828
  • 4
  • 19
  • 40
0

You can use keypress event also like (keypress)="myFunction ($event) " in HTML

In TS File :

myFunction($event) {

Console.log($event.keycode) // here you will get the keycode of enter keyword.

If ( $ event.keycode == "13" ) {// just check it once whether it is 13 or not

this.process(): // whatever yours action perform from here

} }