You can simulate a fake user input by calling the dispatchEvent function (see also this StackOverflow thread). However, faking the holding down a key for five seconds could be problematic. Of course, you could use a setInterval
function to re-dispatch the keyboard event every x
milliseconds:
setInterval(() => {
document.dispatchEvent(keyboardEvent);
}, 100);
But instead you should consider thinking about a different solution without simulating the Space down event at all. Which part of your code would be affected if the Space key is pressed? Just rewrite that piece of code so that it performs a special action when the page is loaded.
It could also be helpful to see some of your script code.