Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {
  }

}
like image 864
user11116445 Avatar asked Dec 08 '25 10:12

user11116445


1 Answers

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

@HostListener('document:keyup', ['$event'])
like image 71
clevertension Avatar answered Dec 10 '25 10:12

clevertension