Learn about the usage of event.keyCode in detail.
event.keyCode is a property used to retrieve the Unicode character encoding value of the key that triggered the keyboard event.
In older versions of JavaScript, it was common practice to use event.keyCode to retrieve the character code of a keyboard event. However, the event.keyCode property has been deprecated and is no longer recommended. It is now recommended to use either event.key or event.code properties to obtain information about keyboard events.
The event.key property returns a string that represents the value of the key that triggered the keyboard event. It will return different values based on the key and the current keyboard layout. For example, pressing the “A” key on an American keyboard layout will return “A”, whereas on a German keyboard layout it will return “Q”.
The event.code property returns a string that represents the unique identifier of the key that triggered the keyboard event. It is not affected by the keyboard layout. For example, pressing the “A” key will always return “KeyA” regardless of the current keyboard layout.
If you still need to use the event.keyCode property to get the character code of a keyboard event, you can use event.which as a replacement. event.which is an alternative to event.keyCode, used to get the character code in some older browsers. However, according to MDN’s recommendation, it is advised to use event.key and event.code instead of event.keyCode and event.which for more accurate and reliable information.
Summary: event.keyCode is deprecated, it is recommended to use event.key or event.code property to retrieve information from keyboard events.