简体   繁体   English

将 window.event 处理程序添加到 typescript

[英]Adding window.event handler to typescript

Normally when I wanted to catch an event on a page in js:通常当我想在 js 中捕获页面上的事件时:

window.onkeydown = function (event) {
    //Do something here
}

I cannot seem to figure out (or Google) how to do this in typescript.我似乎无法弄清楚(或谷歌)如何在 typescript 中执行此操作。 For the setup I am working in, there is a ts file for the page, and a ts file for the class that it is loading.对于我正在使用的设置,页面有一个ts文件,它正在加载的 class 有一个ts文件。

This 这个

window.addEventListener('keydown', keyDownListener, false)

window is defined will all events in lib.d.ts and this particular listener as 定义window将所有lib.d.ts事件以及此特定的侦听器

 addEventListener(type: "keydown", listener: (ev: KeyboardEvent) => any, useCapture?: boolean): void;

or this, if you want to keep your original "style", 或者,如果您想保留原始的“样式”,

window.onkeydown = (ev: KeyboardEvent): any => {
     //do something
}

To answer more clearly: 为了更清楚地回答:

const controlDown = (event: KeyboardEvent) => {
    console.log(event);
};
window.addEventListener('keydown', controlDown);
windows.addEventListener('keydown', (event: KeyboardEvent) =>{
 // if you need event.code
});

windows.addEventListener('keydown', (event: Event) =>{
 // event
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM