user11116445
user11116445

Reputation: 33

execute function on any key press angular

so my dad just entered hospice care and is currently starting the dying process in our dinning room.

Since he is out of the hospital he no longer has access to a way to alert people if he needs help.

I am building an angular app to alert us.

I am trying to bind a function to any press of the keyboard.

However I am not finding anything in my research.

I tried the following method. But without effect.

assistance greatly appreciated.

import {Component, HostListener, OnInit} from '@angular/core';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {

  constructor() { }


  @HostListener('window.keyup', ['$event'])
  keyEvent(event: KeyboardEvent){
    console.log('it worked');
  }

  ngOnInit() {
  }

}

Upvotes: 3

Views: 1671

Answers (2)

rajat gangwani
rajat gangwani

Reputation: 34

you can't detect keypress using window object .Use document instead to detect.Will help you for sure :)

@HostListener('document:keyup', ['$event'])

Upvotes: 0

clevertension
clevertension

Reputation: 7077

'window.keyup' cannot detect the keypress, please try

@HostListener('document:keyup', ['$event'])

Upvotes: 2

Related Questions