Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript 1.5 and window.event.ctrlKey

For debugging purposes I sometimes check to see if ctrl key is held down for special 'secret' actions. I may do this in an arbitrary function that isn't necessary an event handler itself (it may be a callback from something or it may be an event handler).

I've been using the following in typescript:

if (window.event.ctrlKey)

Suddenly now with Visual Studio 2015 RTM (I assume this is TS 1.5) this is not allowed due to ctrlKey not being in the event object anymore.

I'm not sure why and I'm more curious than anything. Is it safe to add it back? Why was it taken away?

[As an aside it turns out this probably doesn't work in Firefox anyway so I'm also looking for a complete cross platform solution]

like image 752
Simon_Weaver Avatar asked Dec 11 '25 19:12

Simon_Weaver


1 Answers

You can easily add that to the interface yourself. e.g. the following code does this :

interface Event{
    ctrlKey : boolean;
}

if (window.event.ctrlKey){

}

Checkout the docs on lib.d.ts : http://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

That said its better if you use a type assertion :

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

}

Checkout the docs on type assertions : http://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html

like image 175
basarat Avatar answered Dec 14 '25 10:12

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!