Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the key property recognised as part of the Event type in typescript

I have a function which finds out what button a user pressed using events, and uses the event.key property. However, in the parameter of the function, if i assign it a type Event, the compiler complains that

Property 'key' does not exist on type 'Event'.

Here is my code.

function getDirection(e:Event):void{
    let directionCode:number = e.key; 
    // code going on here
}

Why isnt the key property recognised on type event.

like image 226
Joe Starbright Avatar asked Mar 04 '19 15:03

Joe Starbright


People also ask

Is event keyCode deprecated?

keyCode. Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.

What should I use instead of an event?

Note: The which property is deprecated. Use the key property instead. The which property returns the Unicode character code of the key that triggered the onkeypress event, or the Unicode key code of the key that triggered the onkeydown or onkeyup event.


1 Answers

Because Event does not have that property, KeyboardEvent is the class you want.

function getDirection(e:KeyboardEvent):void{
    let directionCode:number = e.keyCode; 
    let directionCodeStr:string = e.key; 
    // code going on here
}
like image 177
Titian Cernicova-Dragomir Avatar answered Oct 12 '22 12:10

Titian Cernicova-Dragomir