Adding a window.event handler to typescript

Usually when I wanted to catch an event on a page in js:

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

I cannot figure out (or Google) how to do this in typescript. For the setting I'm working in, there is a file tsfor the page and a file tsfor the class that it loads.

+4
source share
1 answer

it

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

windowall events are defined in lib.d.tsand this particular listener as

 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
}
+4
source

Source: https://habr.com/ru/post/1649539/


All Articles