전체 페이지에서 키 누르기 이벤트를 수신하려면 어떻게해야합니까?
내 전체 페이지에 함수를 바인딩하는 방법을 찾고 있습니다 (사용자가 키를 누를 때 내 구성 요소에서 함수를 트리거하고 싶습니다).
AngularJS에서 a를 사용하는 것은 쉬웠 ng-keypress
지만 (keypress)="handleInput($event)"
.
전체 페이지에서 div 래퍼로 시도했지만 작동하지 않는 것 같습니다. 초점이있을 때만 작동합니다.
<div (keypress)="handleInput($event)" tabindex="1">
구성 요소 내에서 @HostListener 데코레이터를 사용 합니다.
import { HostListener } from '@angular/core';
@Component({
...
})
export class AppComponent {
@HostListener('document:keypress', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
this.key = event.key;
}
}
다음과 같은 다른 옵션도 있습니다.
@Component
데코레이터 내 호스트 속성
Angular는 @HostListener
호스트 속성 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();
}
}
yurzui's answer didn't work for me, it might be a different RC version, or it might be a mistake on my part. Either way, here's how I did it with my component in Angular2 RC4 (which is now quite outdated).
@Component({
...
host: {
'(document:keydown)': 'handleKeyboardEvents($event)'
}
})
export class MyComponent {
...
handleKeyboardEvents(event: KeyboardEvent) {
this.key = event.which || event.keyCode;
}
}
Just to add to this in 2019 w Angular 8,
instead of keypress I had to use keydown
@HostListener('document:keypress', ['$event'])
to
@HostListener('document:keydown', ['$event'])
Working Stacklitz
If you want to perform any event on any specific keyboard button press, in that case, you can use @HostListener. For this, you have to import HostListener in your component ts file.
import { HostListener } from '@angular/core';
then use below function anywhere in your component ts file.
@HostListener('document:keyup', ['$event'])
handleDeleteKeyboardEvent(event: KeyboardEvent) {
if(event.key === 'Delete')
{
// remove something...
}
}
참고URL : https://stackoverflow.com/questions/37362488/how-can-i-listen-for-keypress-event-on-the-whole-page
'programing tip' 카테고리의 다른 글
web.config 파일에서 appSettings 섹션을 읽는 방법은 무엇입니까? (0) | 2020.09.16 |
---|---|
Spring MVC에서 JSON으로 보내는 동안 Java 객체의 필드를 동적으로 무시 (0) | 2020.09.16 |
JSX react / react-in-jsx-scope를 사용할 때 'React'가 범위 내에 있어야합니까? (0) | 2020.09.16 |
ArrayList를 어떻게 변환 할 수 있습니까? (0) | 2020.09.16 |
ngModel을 사용하는 Angular 2 양방향 바인딩이 작동하지 않습니다. (0) | 2020.09.16 |