Shift key pressed? event.shiftKey == 57

Can Someone Explain the Functionality of this Piece of Code?

I recently started learning programming with the Meta-Course on Coursera and thought it would be a good idea to review other people’s codes on GitHub to get some feeling and understanding for whole projects.

I’m having difficulty understanding this piece of code:

document.addEventListener("keydown", function (event) {
  if (event.shiftKey == 57) {
    event.key = "(";

afaik shiftKey always returns a boolean value - so how does this work? (And yes it really does). Can anyone explain this to me? I’ve tried to research it for over 2 hours.

The code you shared adds an event listener to the document object for the “keydown” event. Whenever a key is pressed on the keyboard, the provided function will be executed.

In the function, it checks if the “shiftKey” property of the event object is equal to 57. This comparison is incorrect because “shiftKey” returns a boolean value (true or false) indicating whether the Shift key was pressed during the event.

To correct the code and make it work as intended, you can modify the comparison to check if the key code of the pressed key is equal to 57. Here’s the corrected code:

document.addEventListener("keydown", function (event) {
  if (event.keyCode === 57) {
    event.key = "(";
  }
});

In this updated code, the “keyCode” property is used to compare the key code of the pressed key with the value 57. If the key code matches, the “key” property of the event object is set to “(”.

Please note that using the “keyCode” property is deprecated in favor of the “key” property. You should consider using the “key” property directly if possible, depending on your requirements.