'How can I disable repetition of an eventListener keydown event in plain JavaScript

I am trying to stop a keydown event from repeating for a game. I am unable to use libraries due to it being part of a school project. I have tried most of the answers I can access but they don't work for my code, I also can't use MDN because it's blocked. Here is the code

     window.addEventListener("keydown", function (e){
      if (e.keyCode == 32) {
       accelerateBy = -0.5
       accelerate()
      
     }
    });


Solution 1:[1]

You may have to use some variable to save the state of you key. Here's an example:

let isKeyDown = false;

document.addEventListener("keydown", (event) => {
    if (event.keyCode == 32) {
        if (isKeyDown) { return; } // If key was already down, don't do anything
        isKeyDown = true;
        // do your stuff here
    }
});

document.addEventListener("keyup", (event) => {
    if (event.keyCode == 32) {
        isKeyDown = false;
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Herobrine