我如何检测 Angular 2 上的任何按键(例如,不一定在输入框中)
目前我设法使用以下代码做到这一点:
import {Component, HostListener} from "@angular/core";
@Component(<any>{
selector: 'foo',
template: `<h1>Foo</h1>`,
})
export class FooComponent {
@HostListener('document:keypress', ['$event'])
keypress(e: KeyboardEvent) {
console.log("Key Up! " + e.key);
}
}
上面的代码对大多数字符都能正常工作,例如字母数字、标点符号、符号等
问题是当按下 SHIFT、CTRL、F1...F12、Tab、ALT 等键时,此方法不会运行。
最佳答案
你必须使用 ('document:keydown')
而不是 ('document:keypress')
来获得 shift、ctrl...
关于angular - 如何检测 Angular 2 中的任何按键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135101/