0

I tried to use the event.which and event.keyCode in my JavaScript code today and got a line ruled across each. I read that both have been deprecated on my VSCode.

Does anyone know the alternatives to them?

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42
Aye
  • 271
  • 3
  • 9
  • Have you checked https://stackoverflow.com/questions/4471582/keycode-vs-which? – raina77ow Oct 10 '20 at 20:27
  • Use [KeyboardEvent.code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) instead. – Jeroen van der Laan Oct 10 '20 at 20:36
  • VSCode is being strict about 'event'. If you add a parameter `function (event)` - or 'ev' or whatever - to your event-handling function, then 'event' becomes available inside the function, rather than the function getting the global 'event', which is one reason VSCode crosses out 'event'. There are others cases, but this one is a common error. – Dave Everitt May 15 '21 at 15:32

2 Answers2

1

To determine what character corresponds with the key event, you need to use event.key instead. The events which/keyCode are indeed deprecated.

Cheers

1

event.keyCode is deprecated , although some browsers do support it. Use event.code instead.

Read this on how to implement crossbrowser for complete support

Manish Kumar
  • 101
  • 11