IT

'keyCode' is deprecated.ts(6385)lib.dom.d.ts(14396, 8): The declaration was marked as deprecated here

bepuri 2023. 9. 18. 10:16
728x90

Beacuse of event.keyCode is deprecated.
It means we are not supposed to use that code in our project.
Then how can we change code properly.

Before.

    document.addEventListener("keyup", (e) => {        
      if (e.keyCode == 37) {
          some code...
      }
    })

It should be updated like this.



    document.addEventListener("keyup", (e) => {        
      if (e.key == "ArrowLeft") {
          some code...
      }
    })

Then we have one question left, how can we know key name?
The easiest way is looking mdn.

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

See for youself.

728x90