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

Issue

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.

Solution

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
}

Answered By – Titian Cernicova-Dragomir

Answer Checked By – Cary Denson (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published