打字稿 1.5 和 window.event.ctrlKey

Typescript 1.5 and window.event.ctrlKey

出于调试目的,我有时会检查 ctrl 键是否为特殊的 'secret' 操作按下。我可以在不需要事件处理程序本身的任意函数中执行此操作(它可能是来自某事的回调,也可能是事件处理程序)。

我一直在打字稿中使用以下内容:

if (window.event.ctrlKey)

突然现在 Visual Studio 2015 RTM(我假设这是 TS 1.5)这是不允许的,因为 ctrlKey 不再在 event 对象中。

我不知道为什么,我比什么都好奇。把它加回去安全吗?为什么被拿走了?

[顺便说一句,这可能在 Firefox 中不起作用,所以我也在寻找一个完整的跨平台解决方案]

您可以自己轻松地将其添加到界面中。例如以下代码执行此操作:

interface Event{
    ctrlKey : boolean;
}

if (window.event.ctrlKey){

}

查看 lib.d.ts 上的文档:http://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

也就是说,如果您使用类型断言会更好:

if ((<KeyboardEvent>window.event).ctrlKey){

}

查看有关类型断言的文档:http://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html