gpt4 book ai didi

angular - 如何检测 Angular 2 中的任何按键?

转载 作者:太空狗 更新时间:2023-10-29 17:19:13 25 4
gpt4 key购买 nike

我如何检测 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com