How can I listen for keypress event on the whole page?

L.querter :

I'm looking for a way to bind a function to my whole page (when a user presses a key, I want it to trigger a function in my component.ts)

It was easy in AngularJS with a ng-keypress but it does not work with (keypress)="handleInput($event)".

I tried it with a div wrapper on the whole page but it doesn't seem to work. it only works when the focus is on it.

<div (keypress)="handleInput($event)" tabindex="1">
yurzui :

I would use @HostListener decorator within your component:

import { HostListener } from '@angular/core';

@Component({
  ...
})
export class AppComponent {

  @HostListener('document:keypress', ['$event'])
  handleKeyboardEvent(event: KeyboardEvent) { 
    this.key = event.key;
  }
}

There are also other options like:

host property within @Component decorator

Angular recommends using @HostListener decorator over host property https://angular.io/guide/styleguide#style-06-03

@Component({
  ...
  host: {
    '(document:keypress)': 'handleKeyboardEvent($event)'
  }
})
export class AppComponent {
  handleKeyboardEvent(event: KeyboardEvent) {
    console.log(event);
  }
}

renderer.listen

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

@Component({
  ...
})
export class AppComponent {
  globalListenFunc: Function;

  constructor(private renderer: Renderer2) {}

  ngOnInit() {
    this.globalListenFunc = this.renderer.listen('document', 'keypress', e => {
      console.log(e);
    });
  }

  ngOnDestroy() {
    // remove listener
    this.globalListenFunc();
  }
}

Observable.fromEvent

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import { Subscription } from 'rxjs/Subscription';

@Component({
  ...
})
export class AppComponent {
  subscription: Subscription;

  ngOnInit() {
    this.subscription = Observable.fromEvent(document, 'keypress').subscribe(e => {
      console.log(e);
    })
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I listen to session destroyed event?

How can I listen for modal close event?

How can I pass a keypress value to a function, through an event listener?

How can I broadcast and pick up a keypress event in AngularJS?

How can I get jquery .val() AFTER keypress event?

How to automate keypress event on web page

How can I render an event to whole cell in full calendar js?

How can I listen to one event from two template handlers?

How can I listen to the scroll event in a sidebar [Bootstrap Vue]

How can I listen click event on Windows notifications?

using localstack AWS how can i listen to SNS event in chalice

How can I register only numbers in TextBox?(i dont have KeyPress event)

How can I make a sizer expand to the whole wxNotebook page?

How can i listen to changes in my observable through my whole app

How can I make a textbox listening to a Keypress-Event and an InputBinding together

Using Javascript, how can I detect a keypress event but NOT when the user is typing in a form field?

How can I get the original key map of a numbered key in a keypress event with shift?

I can not find KeyPress event handler in c#

Where can I find documentation of the 'keypress' event in Node.js

How can I simulate a keypress in JavaScript?

How can i make a keypress action to this code?

how can i mute an iframe on keypress

My data table not fix to page How can I show my table in whole page?

how do I remove a keypress event added in <html>?

How do i click button from keypress event

How do I make the backspace work normally in the keypress event?

How can I listen for an event based on a common class/ object name instead of a unique layer in Framer JS?

How can I listen for the "timeupdate" event from my video.js player?

jQuery change event not firing on page load, how can I fix it?