且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测TypeScript中按下的键?

更新时间:2023-12-03 10:25:10

需要使用更专业的事件类型KeyboardEvent,如下图:

You need to use the more specialised event type KeyboardEvent, as shown below:

myFunc(data : string, evt : KeyboardEvent)

如果您还想删除 evt.enterKey 的错误,您需要通过扩展接口来添加它 - 尽管我不知道这是一个真实的属性,因为它不是技术上的控制键,例如 CTRLSHIFTALT,它们都具有事件属性:

If you want to also remove errors for evt.enterKey you'll need to add it by extending the interface - although I'm not aware that this is a real property as it isn't technical a control key, like CTRL, SHIFT or ALT, which all have properties on the event:

interface KeyboardEvent {
    enterKey: boolean;
}